I have an applet, that when it starts up it has a riddle and a little text field for the user to enter the answer. What i want the program to do is that when the user hits the "Enter Answer" button, it clears everything and loads the things from another class.
Here is the code:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class threeRiddles extends Applet implements ActionListener
{
public Font nameFont;
public Font buttonFont;
public Font buttonFont2;
public Image pic;
public Button BNQ;
public Font normalFont;
public String answerToRiddle;
public String riddlel1;
public String riddlel2;
public String riddlel3;
public String riddlel4;
public String riddlel5;
public String riddlel6;
public Label riddleLA1;
public Label riddleLA2;
public Label riddleLA3;
public Label riddleLA4;
public Label riddleLA5;
public Label riddleLA6;
public TextField answerRiddle;
public void init()
{
setLayout(null);
setSize(500,680);
setBackground (Color.blue);
normalFont = new Font ("Times New Roman", Font.BOLD, 14);
buttonFont = new Font ("Dialog", Font.BOLD, 12);
buttonFont2 = new Font ("Dialog", Font.BOLD, 12);
nameFont = new Font ("SansSerif", Font.BOLD, 28);
pic = getImage(getCodeBase(), "albert14.gif");
riddlel1 = "This thing all things devours;";
riddlel2 = "Birds, beasts, trees, flowers;";
riddlel3 = "Gnaws irons, bites steel;";
riddlel4 = "Grinds hard stones to meal;";
riddlel5 = "Slays king, ruins town,";
riddlel6 = "And beats high mountain down.";
answerRiddle = new TextField();
answerRiddle.setBounds(135,180,75,20);
add(answerRiddle);
BNQ = new Button("Quit");
BNQ.setBounds(135,210,100,36);
BNQ.setFont(buttonFont);
BNQ.setBackground(Color.yellow);
BNQ.addActionListener(this);
add(BNQ);
riddleLA1 = new Label(riddlel1);
riddleLA1.setBounds(20,70,240,20);
riddleLA1.setFont(normalFont);
add(riddleLA1);
riddleLA2 = new Label(riddlel2);
riddleLA2.setBounds(20,85,240,20);
riddleLA2.setFont(normalFont);
add(riddleLA2);
riddleLA3 = new Label(riddlel3);
riddleLA3.setBounds(20,100,240,20);
riddleLA3.setFont(normalFont);
add(riddleLA3);
riddleLA4 = new Label(riddlel4);
riddleLA4.setBounds(20,115,240,20);
riddleLA4.setFont(normalFont);
add(riddleLA4);
riddleLA5 = new Label(riddlel5);
riddleLA5.setBounds(20,130,240,20);
riddleLA5.setFont(normalFont);
add(riddleLA5);
riddleLA6 = new Label(riddlel6);
riddleLA6.setBounds(20,145,240,20);
riddleLA6.setFont(normalFont);
add(riddleLA6);
}
public void paint(Graphics g)
{
g.setColor (Color.red);
g.setFont (nameFont);
g.drawString("Java Riddle Game", 20, 40);
g.drawString("Clue...", 90, 270);
g.drawImage(pic, 90, 275, this);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
String answerToRiddle = answerRiddle.getText();
If (command.equals("time"));
{
}
}
}
I have commented out the image rendering (since I dont have it here). This
example shows how to clear out the old contents and displays a new
panel after the Quit button is clicked. It's obviously not what you want to
happend, but it illustrates the swapping of guis.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class RiddlesPanel extends Panel {
public Button BNQ;
public String answerToRiddle;
public String riddlel1;
public String riddlel2;
public String riddlel3;
public String riddlel4;
public String riddlel5;
public String riddlel6;
public Label riddleLA1;
public Label riddleLA2;
public Label riddleLA3;
public Label riddleLA4;
public Label riddleLA5;
public Label riddleLA6;
public TextField answerRiddle;
public ThreeRiddles applet=null;
public RiddlesPanel (ThreeRiddles applet) {
this.applet=applet;
setLayout(null);
setBackground(Color.blue);
setup();
}
private void setup() {
riddlel1 = "This thing all things devours;";
riddlel2 = "Birds, beasts, trees, flowers;";
riddlel3 = "Gnaws irons, bites steel;";
riddlel4 = "Grinds hard stones to meal;";
riddlel5 = "Slays king, ruins town,";
riddlel6 = "And beats high mountain down.";
answerRiddle = new TextField();
answerRiddle.setBounds(135, 180, 75, 20);
add(answerRiddle);
BNQ = new Button("Quit");
BNQ.setBounds(135, 210, 100, 36);
BNQ.setFont(ThreeRiddles.buttonFont);
BNQ.setBackground(Color.yellow);
BNQ.addActionListener(applet);
add(BNQ);
riddleLA1 = new Label(riddlel1);
riddleLA1.setBounds(20, 70, 240, 20);
riddleLA1.setFont(ThreeRiddles.normalFont);
add(riddleLA1);
riddleLA2 = new Label(riddlel2);
riddleLA2.setBounds(20, 85, 240, 20);
riddleLA2.setFont(ThreeRiddles.normalFont);
add(riddleLA2);
riddleLA3 = new Label(riddlel3);
riddleLA3.setBounds(20, 100, 240, 20);
riddleLA3.setFont(ThreeRiddles.normalFont);
add(riddleLA3);
riddleLA4 = new Label(riddlel4);
riddleLA4.setBounds(20, 115, 240, 20);
riddleLA4.setFont(ThreeRiddles.normalFont);
add(riddleLA4);
riddleLA5 = new Label(riddlel5);
riddleLA5.setBounds(20, 130, 240, 20);
riddleLA5.setFont(ThreeRiddles.normalFont);
add(riddleLA5);
riddleLA6 = new Label(riddlel6);
riddleLA6.setBounds(20, 145, 240, 20);
riddleLA6.setFont(ThreeRiddles.normalFont);
add(riddleLA6);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.setFont(ThreeRiddles.nameFont);
g.drawString("Java Riddle Game", 20, 40);
g.drawString("Clue...", 90, 270);
//g.drawImage(pic, 90, 275, this);
}
}
class AnotherPanel extends Panel {
public void paint(Graphics g) {
g.setColor(Color.red);
g.setFont(ThreeRiddles.nameFont);
g.drawString("And here is the other panel", 20, 40);
}
}
public class ThreeRiddles extends Applet implements ActionListener {
public static Font nameFont=new Font("SansSerif", Font.BOLD, 28);
public static Font buttonFont=new Font("Dialog", Font.BOLD, 12);
public static Font buttonFont2=new Font("Dialog", Font.BOLD, 12);
public static Font normalFont=new Font("Times New Roman", Font.BOLD, 14);
//public Image pic;
RiddlesPanel riddlesPan=null;
AnotherPanel anotherPanel=null;
public void init() {
setLayout(new GridLayout());
riddlesPan=new RiddlesPanel(this);
add(riddlesPan);
setSize(500, 680);
}
//pic = getImage(getCodeBase(), "albert14.gif");
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
String answerToRiddle = riddlesPan.answerRiddle.getText();
// remove riddlespanel and show anotherpanel
anotherPanel=new AnotherPanel();
replacePanel(riddlesPan, anotherPanel);
}
public void replacePanel(Panel oldPanel, Panel newPanel) {
Component [] cmp=this.getComponents();
for (int i=0; i<cmp.length; i++) {
if (cmp[i]==oldPanel) {
this.remove(oldPanel);
break;
}
}
this.add(newPanel);
validate();
repaint();
}
}
I meant so the Once the user enters the correct answer to the riddle,
it should load everything from another class. In this other class, everything is similar, except for captions/pictures.
Then what you need is not a new class, but a class (like the one you
already have) that is capable of loading "sets" of parameters, where a "set"
is the info required for a complete screenload (riddle)
I have hacked up a setup here, I haven't tested it yet ...,
but I'll do that later today, I bet it crashes BIGTIME now ..
The java source and html is attatched. It still has a few quirks, like
too short labels for some lines, and it lacks the code for answer checking.
I figured that could be a nice thing for you to do. Also, a "Quit" button
is an appilcation thing, on an applet like this it should be "Submit answer" or
something like that.
Bookmarks