import java.awt.Component; /***************************************** * An class that handles the specifics * of animation. This class can be used to * animate a component *****************************************/ public class Animator implements Runnable{ public Animator(Component comp){ this.comp = comp; } public void start(){ if (animationThread == null){ animationThread = new Thread(this); animationThread.start(); } } public void stop(){ animationThread = null; } public void run(){ while (Thread.currentThread() == animationThread){ comp.repaint(); try{ Thread.currentThread().sleep(delay); } catch(InterruptedException e){/* don't do anything*/} } } // Java bean pattern to enforce encapsulation public void setDelay(int delay){ this.delay = delay; } // Java bean pattern to enforce encapsulation public int getDelay(){ return delay; } // the thread that does the redrawing private Thread animationThread = null; // how often the thread redraws itself private int delay = 100; // the component to be animated private Component comp; }