Help with repaint() and init()...

M

michael.rygh

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("");
}




}
}
 
O

Oliver Wong

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..
[code snipped]

Rather than erasing the current button and putting up new ones, why not
just change the text on the buttons and re-use them?

- Oliver
 
T

Thomas Hawtin

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!

I suggest you introduce a panel each for the two screens. Give the
Applet (or a panel added to it) CardLayout as the layout manager. Use
the CardLayout to switch between the two views. Alternatively you can
use a BorderLayout and setVisible true/false on the two panels yourself.
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

That's a lot of classes you are importing. There's even at least one
name clash.
public class quiz extends Applet implements ActionListener, Runnable
{

I suggest you don't implement interfaces like this. Anonymous inner
classes make things much easier to read. Also class names should be in
initial caps.
Thread appletThread;

I'd suggest avoiding threads if you possible can. If you use
javax.swing.Timer then you can keep to the Event Dispatch Thread (even
using AWT instead of Swing it is worth doing). Kicking it off in the
init wrecks the idea of keeping to the EDT, so wrap the init (and
destroy) code in the usual invokeAndWait boilerplate. It is also best to
start and stop the timer/thread in the start and stop methods.
[...lots of fields...]

Most of these could be local variables. Local variables tend to make it
easier to rearrange your code.
//override the init() method to initialize and start a thread of
execution

From 1.5 you can write @Override. For stuff to run in 1.4, I tend to
write it with @Override to check and then comment it out when I change
-source and -target.
public void init()
{
setFont(font1);
setLayout(null);

You'll find it much easier using layout managers.
catch(InterruptedException i){System.exit(1);}

That's not going to work in an applet (although I haven't tried exiting
a browser from a signed applet).
public void paint(Graphics g)
{
[...]
red = !red;

Paints can be triggered by the operating system as well as through
repaint. So it's generally a good idea not to alter state (except
caches) within paint.
public void actionPerformed(ActionEvent event)
{

Graphics g = getGraphics();

if (event.getActionCommand().equals("OK"))

It's generally better to use separate ActionListener anonymous inner
classes than testing event action command/sources. Certainly comparing
literal strings is never good.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top