This version of the applet implements the interface Runnable to do it's animation. This allows us to use a wait command to create our delay.

So here's what to write down:

public class MyAnimation implements Runnable //make your class implement Runnable
{
Thread anim; //this is global
.
. //Your other code
.
public void start() //starts the animation, if you put it in an applet and call it
{ //start(), though, it will start it with the applet, so if you don't
anim = new Thread(this) //want that to happen, call it something else.
anim.start();
} //relatively painless, right?
public synchronized void stop () //this is how to stop the animation- the method doesn't need to be called
{ //stop(), but the advantage is that it will call stop when you close the
anim = null; //applet, automatically stopping the animation for you (you're overriding
notify(); //stop() in Applet, it has nothing to do with Runnable). It should be
} //synchronized which means it can be executed at the same time as other
//synchronized methods.
public synchronized void run() //This is the method you have to write to use Runnable.
{
.
. //Whatever initialization you want to do first
.
while (Thread.currentThread()==anim) //key line, even if you don't remember what it does, remember it.
{
. //here are the things happening in your animation, modifications, increment
. //counters, do some painting or call repaint()
.
try //You still haven't learned to use try/catch statements, just trust me for
{ //now on this.
wait(50); //the parameter in wait is the delay in milliseconds
}
catch (InterruptedException e)
{
break; //if the animation is being interrupted (like when the window is closed)
} //leave the while loop and end the run method
}
}
}