home *** CD-ROM | disk | FTP | other *** search
/ Datatid 2000 #1 / Datatid-2000-01.iso / Internet / JAVA_NAVIGATOR / JAVANAVIGATOR.EXE / %MAINDIR% / files / JClass / Timer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-31  |  2.7 KB  |  115 lines

  1. import java.awt.Component;
  2. import java.awt.Event;
  3.  
  4. public class Timer implements Runnable {
  5.    Component target;
  6.    int eventType;
  7.    boolean repeat;
  8.    boolean repeating;
  9.    boolean execute;
  10.    Thread thread;
  11.    int delay;
  12.  
  13.    public Timer(Component t) {
  14.       this(t, 1000);
  15.    }
  16.  
  17.    public Timer(Component t, int d) {
  18.       this(t, d, false);
  19.    }
  20.  
  21.    public Timer(Component t, int d, boolean r) {
  22.       this(t, d, r, 1001);
  23.    }
  24.  
  25.    public Timer(Component t, int d, boolean r, int e) {
  26.       this.target = t;
  27.       this.delay = d;
  28.       this.repeat = r;
  29.       this.execute = false;
  30.       this.thread = new Thread(this);
  31.       this.eventType = e;
  32.       this.thread.start();
  33.    }
  34.  
  35.    public void setEventType(int type) {
  36.       this.eventType = type;
  37.    }
  38.  
  39.    public int getEventType() {
  40.       return this.eventType;
  41.    }
  42.  
  43.    public void setTarget(Component t) {
  44.       this.target = t;
  45.    }
  46.  
  47.    public Component getTarget() {
  48.       return this.target;
  49.    }
  50.  
  51.    public void setDelay(int d) {
  52.       this.delay = d;
  53.    }
  54.  
  55.    public int getDelay() {
  56.       return this.delay;
  57.    }
  58.  
  59.    public void start() {
  60.       this.execute = true;
  61.       this.thread.resume();
  62.    }
  63.  
  64.    public void setRepeat(boolean f) {
  65.       this.repeat = f;
  66.    }
  67.  
  68.    public boolean getRepeat() {
  69.       return this.repeat;
  70.    }
  71.  
  72.    public void start(int d) {
  73.       this.delay = d;
  74.       this.start();
  75.    }
  76.  
  77.    public void start(boolean r) {
  78.       this.repeat = r;
  79.       this.start();
  80.    }
  81.  
  82.    public void start(int d, boolean r) {
  83.       this.delay = d;
  84.       this.repeat = r;
  85.       this.start();
  86.    }
  87.  
  88.    public void stop() {
  89.       this.execute = false;
  90.       this.repeating = false;
  91.    }
  92.  
  93.    public void run() {
  94.       if (!this.execute) {
  95.          this.thread.suspend();
  96.       }
  97.  
  98.       try {
  99.          while(true) {
  100.             this.repeating = this.repeat;
  101.  
  102.             do {
  103.                Thread.sleep((long)this.delay);
  104.                if (this.execute) {
  105.                   this.target.handleEvent(new Event(this, this.eventType, (Object)null));
  106.                }
  107.             } while(this.repeating);
  108.  
  109.             this.thread.suspend();
  110.          }
  111.       } catch (InterruptedException var1) {
  112.       }
  113.    }
  114. }
  115.