I guess I'm just confused how they really work. I know to some of you
this will be easy to explain and once I understand it I'll be fine but
I'm just not exactly sure how they work.
I'm trying to write a quiz program where the first thing on the applet
is for them to enter their name and hit ok. Then after they hit ok, it
will bring up the first quiz question.. like I'll have a picture then
they get 4 buttons and they each have a name and if they click the
right one they get so many points... anyway..
I have it show the first page... but what in my code do I need to do
once they hit th OK button, to erase the current buttons on the screen
and the text so that I can have a blank sheet to work with and put up
the picture and questions and new buttons? I hope you understand..
thank you very much for your time!
here is what i have so far..
//quiz program
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class quiz extends Applet implements ActionListener, Runnable
{
Thread appletThread;
AudioClip sound;
Font font = new Font("Monospaced",Font.BOLD,34);
Font font1 = new Font("Monospaced",Font.BOLD,20);
Font font2 = new Font("SansSerif",Font.ITALIC,20);
TextField box1 = new TextField(20);
String fname = "";
String lname = "";
boolean alarm;
boolean red = true;
Button button1;
Button button2;
Button button3;
Button button4;
//override the init() method to initialize and start a thread
of
execution
public void init()
{
setFont(font1);
setLayout(null);
setBackground(Color.PINK);
Label prompt = new Label ("Enter your first name and
hit OK: ");
add(prompt);
add(box1);
box1.addActionListener(this);
prompt.setBounds(100,100,400,50);
box1.setBounds(200,200,150,30);
if (appletThread ==null)
{
appletThread = new Thread(this);
appletThread.start(); //start from class Thread
}
//create buttons
button1 = new Button("OK");
button1.setLocation(200, 250);
button1.setSize(60,40);
button1.setForeground(Color.black);
button1.setBackground(Color.white);
button1.addActionListener(this);
add(button1);
button2 = new Button("CLEAR");
button2.setLocation(275, 250);
button2.setSize(70,40);
button2.setForeground(Color.black);
button2.setBackground(Color.white);
button2.addActionListener(this);
add(button2);
setFont(font2);
Label prompt1 = new Label ("Written and Programmed
by: ");
add(prompt1);
prompt1.setBounds(200,400,350,50);
Label prompt2 = new Label ("Michael Rygh");
add(prompt2);
prompt2.setBounds(300,450,150,50);
}
public void run() //implemented from the interface runnable
{
while (true)
{
repaint();
//generate a short pause by letting the thread
sleep
try{Thread.sleep(1000);}
catch(InterruptedException i){System.exit(1);}
}
}
//override the destroy() method to stop the execution of the
thread
and nullify the thread
public void destory()
{
if (appletThread !=null)
{
appletThread.stop();
appletThread = null;
}
}
public void paint(Graphics g)
{
g.setFont(font);
g.drawRect(50, 50, 500, 500);
if (red) //toggle current drawing color
{
g.setColor(Color.WHITE);
}
else
{
g.setColor(Color.black);
}
red = !red;
g.drawString("THE QUIZ GAME", 150,50);
}
public void actionPerformed(ActionEvent event)
{
Graphics g = getGraphics();
if (event.getActionCommand().equals("OK"))
{
fname = box1.getText();
repaint();
System.out.println(fname);
System.out.println("it repainted");
}
if (event.getActionCommand().equals("CLEAR"))
{
box1.setText("");
}
}
Roedy Green - 12 Dec 2005 23:05 GMT
See http://mindprod.com/jgloss/paint.html
and follow links to get an overview of what is happening.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Paulus de Boska - 13 Dec 2005 21:08 GMT
Hi,
you can clean out a Container like Applet by using removeAll(), then
fill it again by adding buttons and other components, don't forget to
write a validate() statement after this, because else the newly added
components won't be shown in the classic AWT (as in your code).
Another solution would be to use different Panels, filled with buttons
a.o., and add such a Panel to the Applet after emptying it. You don't
have to code paint/repaint where controls/components are concerned,
just don't forget validate() after adding or otherwise changing the
Container's contents.
Paul Hamaker
SEMM
http://javalessons.com