//SpinBall.java //Demonstrates animation loop //Andy Harris, 06/00 import java.awt.*; import java.applet.*; public class SpinBall extends Applet implements Runnable { Image ball[] = new Image[6]; Thread looper; int counter = 1; boolean keepGoing = true; public void init(){ ball[1] = getImage(getCodeBase(), "jball1.gif"); ball[2] = getImage(getCodeBase(), "jball2.gif"); ball[3] = getImage(getCodeBase(), "jball3.gif"); ball[4] = getImage(getCodeBase(), "jball4.gif"); ball[5] = getImage(getCodeBase(), "jball5.gif"); } // end init public void start(){ if (looper == null){ looper = new Thread(this); keepGoing = true; looper.start(); } // end if } // end start public void stop(){ keepGoing = false; looper = null; } // end stop public void run(){ while(keepGoing){ try { looper.sleep(250); } catch (InterruptedException exc){ System.out.println(exc.getMessage()); } // end try counter++; if (counter > 5){ counter = 1; } // end if repaint(); } // end while } // end run public void paint(Graphics g){ g.drawImage(ball[counter], 10, 10, this); } // end paint } // end class def