home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Internet / Java / ui / drawing / example / AnimatorApplication.class (.txt) < prev    next >
Encoding:
Java Class File  |  1978-03-06  |  2.3 KB  |  100 lines

  1. import java.awt.Component;
  2. import java.awt.Event;
  3. import java.awt.Frame;
  4. import java.awt.Graphics;
  5. import java.awt.Window;
  6.  
  7. public class AnimatorApplication extends Frame implements Runnable {
  8.    int frameNumber = -1;
  9.    int delay;
  10.    Thread animatorThread;
  11.    boolean frozen = false;
  12.  
  13.    AnimatorApplication(int var1, String var2) {
  14.       super(var2);
  15.       this.delay = var1 > 0 ? 1000 / var1 : 100;
  16.    }
  17.  
  18.    public void startAnimation() {
  19.       if (!this.frozen) {
  20.          if (this.animatorThread == null) {
  21.             this.animatorThread = new Thread(this);
  22.          }
  23.  
  24.          this.animatorThread.start();
  25.       }
  26.  
  27.    }
  28.  
  29.    public void stopAnimation() {
  30.       this.animatorThread = null;
  31.    }
  32.  
  33.    public boolean mouseDown(Event var1, int var2, int var3) {
  34.       if (this.frozen) {
  35.          this.frozen = false;
  36.          this.startAnimation();
  37.       } else {
  38.          this.frozen = true;
  39.          this.stopAnimation();
  40.       }
  41.  
  42.       return true;
  43.    }
  44.  
  45.    public boolean handleEvent(Event var1) {
  46.       switch (var1.id) {
  47.          case 201:
  48.             System.exit(0);
  49.          case 202:
  50.          default:
  51.             break;
  52.          case 203:
  53.             this.stopAnimation();
  54.             break;
  55.          case 204:
  56.             this.startAnimation();
  57.       }
  58.  
  59.       return super.handleEvent(var1);
  60.    }
  61.  
  62.    public void run() {
  63.       Thread.currentThread().setPriority(1);
  64.       long var1 = System.currentTimeMillis();
  65.  
  66.       while(Thread.currentThread() == this.animatorThread) {
  67.          ++this.frameNumber;
  68.          ((Component)this).repaint();
  69.  
  70.          try {
  71.             var1 += (long)this.delay;
  72.             Thread.sleep(Math.max(0L, var1 - System.currentTimeMillis()));
  73.          } catch (InterruptedException var3) {
  74.             return;
  75.          }
  76.       }
  77.  
  78.    }
  79.  
  80.    public void paint(Graphics var1) {
  81.       var1.drawString("Frame " + this.frameNumber, 5, 50);
  82.    }
  83.  
  84.    public static void main(String[] var0) {
  85.       Object var1 = null;
  86.       int var2 = 10;
  87.       if (var0.length > 0) {
  88.          try {
  89.             var2 = Integer.parseInt(var0[0]);
  90.          } catch (Exception var3) {
  91.          }
  92.       }
  93.  
  94.       AnimatorApplication var4 = new AnimatorApplication(var2, "Animator");
  95.       ((Component)var4).resize(200, 60);
  96.       ((Window)var4).show();
  97.       var4.startAnimation();
  98.    }
  99. }
  100.