GUI repaint / loop problem

The goal of this is to draw a cascading or falling ball. The main issue seems to be in the paint method as as I can't figure out how to erase the ball before redrawing the next one, the "Erase()" is a poor attempt to fix this and it doesn't work.

Code is below.

Thanks!

import java.awt.*;

import java.applet.Applet;

publicclass BouncingBallAppletextends Appletimplements Runnable

{

publicfinalint width = 100;

publicfinalint height = 200;

publicfinalint delay = 500;

Thread aThread;

// The current frame:

int frameNumber;

publicvoid init ()

{

aThread =null;

}

publicvoid start ()

{

if (aThread ==null)

{

aThread =new Thread (this);

}

aThread.start ();

}

publicvoid run ()

{

while (aThread !=null)

{

// Draw next frame.

}

}

publicvoid erase()

{

canvas.repaint();

}

publicvoid paint (Graphics g)

{

// Set foreground and background colors.

setBackground (Color.blue);

g.setColor (Color.red);

int balls = 0;

for(;;)

{

if (frameNumber == 1)

{

g.fillOval (width/2, 0, 90, 90);

frameNumber++;

erase();

}

if (frameNumber == 2 )

{

g.fillOval (width/2, height/2, 90, 90);

frameNumber++;

erase();

}

else

{

g.fillOval (width/2, height, 90, 90);

frameNumber = 1;

erase();

}

try

{

Thread.sleep (delay);

}

catch (Exception e)

{

}

}

}

}

[3857 byte] By [indigopimaca] at [2007-11-27 1:38:46]
# 1
don't forget to call super.paint(g) as a first statement when you override paint methodEdit: + you should'nt add loops in a paint method...
calvino_inda at 2007-7-12 0:51:00 > top of Java-index,Java Essentials,Java Programming...
# 2
You should do the iteration in the run method of the thread. There you can modify the coordinates of the ball ant than call the repaint method. When you overwrite the paint method call clearRect(...).
J@A@V@Aa at 2007-7-12 0:51:00 > top of Java-index,Java Essentials,Java Programming...