I'm not quite certain what you mean by this.
Do you mean clear the drawing area and the text components, or load
another applet page, or do you just want to switch to another panel, or... ?
Ok, I've tried to guess it...
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();
}
}