Source of Animator.java


  1: import java.awt.*;
  2: 
  3: public class Animator 
  4:     implements Runnable {  
  5:   public Animator(Component comp) {
  6:     this.comp = comp; 
  7:   } 
  8:   
  9:   final public void setDelay(int delay) {
 10:     this.delay = delay; 
 11:   }
 12:   
 13:   final public int getDelay() {
 14:     return delay; 
 15:   }
 16:   public void start() {
 17:     animationThread = new Thread(this);
 18:     animationThread.start();
 19:   }
 20:     
 21:   public void stop() {
 22:     animationThread = null; 
 23:   }
 24:   
 25:   public void run() {
 26:     while (Thread.currentThread() == animationThread) {
 27:       try { 
 28:         Thread.sleep(delay); 
 29:       } catch (InterruptedException e){}     
 30:       comp.repaint();
 31:     }
 32:   }
 33:   
 34:   protected Component comp; 
 35:   /** The interval between two consecutive frames 
 36:       in milliseconds */
 37:   protected int delay = 100;     
 38:   /** The animation thread */ 
 39:   protected Thread animationThread;
 40:   
 41: }