Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
michael.rygh@gmail.com - 12 Dec 2005 21:44 GMT
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("");
        }
       
   
       
       
    }
}
Oliver Wong - 12 Dec 2005 22:39 GMT
>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
[quoted text clipped - 13 lines]
>
> 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
Thomas Hawtin - 12 Dec 2005 22:45 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.