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)
{
}
}
}
}

