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

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9.  
  10. public class MTImageSequence extends Applet implements Runnable {
  11.    int frameNumber = -1;
  12.    int delay;
  13.    Thread animatorThread;
  14.    boolean frozen = false;
  15.    Dimension offDimension;
  16.    Image offImage;
  17.    Graphics offGraphics;
  18.    Image[] images;
  19.    MediaTracker tracker;
  20.  
  21.    public void init() {
  22.       int var2 = 10;
  23.       String var1 = ((Applet)this).getParameter("fps");
  24.  
  25.       try {
  26.          if (var1 != null) {
  27.             var2 = Integer.parseInt(var1);
  28.          }
  29.       } catch (Exception var4) {
  30.       }
  31.  
  32.       this.delay = var2 > 0 ? 1000 / var2 : 100;
  33.       this.images = new Image[10];
  34.       this.tracker = new MediaTracker(this);
  35.  
  36.       for(int var3 = 1; var3 <= 10; ++var3) {
  37.          this.images[var3 - 1] = ((Applet)this).getImage(((Applet)this).getCodeBase(), "../../../images/duke/T" + var3 + ".gif");
  38.          this.tracker.addImage(this.images[var3 - 1], 0);
  39.       }
  40.  
  41.    }
  42.  
  43.    public void start() {
  44.       if (!this.frozen) {
  45.          if (this.animatorThread == null) {
  46.             this.animatorThread = new Thread(this);
  47.          }
  48.  
  49.          this.animatorThread.start();
  50.       }
  51.  
  52.    }
  53.  
  54.    public void stop() {
  55.       this.animatorThread = null;
  56.       this.offGraphics = null;
  57.       this.offImage = null;
  58.    }
  59.  
  60.    public boolean mouseDown(Event var1, int var2, int var3) {
  61.       if (this.frozen) {
  62.          this.frozen = false;
  63.          this.start();
  64.       } else {
  65.          this.frozen = true;
  66.          this.animatorThread = null;
  67.       }
  68.  
  69.       return true;
  70.    }
  71.  
  72.    public void run() {
  73.       try {
  74.          this.tracker.waitForAll();
  75.       } catch (InterruptedException var4) {
  76.       }
  77.  
  78.       Thread.currentThread().setPriority(1);
  79.       long var1 = System.currentTimeMillis();
  80.  
  81.       while(Thread.currentThread() == this.animatorThread) {
  82.          ++this.frameNumber;
  83.          ((Component)this).repaint();
  84.  
  85.          try {
  86.             var1 += (long)this.delay;
  87.             Thread.sleep(Math.max(0L, var1 - System.currentTimeMillis()));
  88.          } catch (InterruptedException var3) {
  89.             return;
  90.          }
  91.       }
  92.  
  93.    }
  94.  
  95.    public void paint(Graphics var1) {
  96.       this.update(var1);
  97.    }
  98.  
  99.    public void update(Graphics var1) {
  100.       Dimension var2 = ((Component)this).size();
  101.       if (!this.tracker.checkAll()) {
  102.          var1.clearRect(0, 0, var2.width, var2.height);
  103.          var1.drawString("Please wait...", 0, var2.height / 2);
  104.       } else {
  105.          if (this.offGraphics == null || var2.width != this.offDimension.width || var2.height != this.offDimension.height) {
  106.             this.offDimension = var2;
  107.             this.offImage = ((Component)this).createImage(var2.width, var2.height);
  108.             this.offGraphics = this.offImage.getGraphics();
  109.          }
  110.  
  111.          this.offGraphics.setColor(((Component)this).getBackground());
  112.          this.offGraphics.fillRect(0, 0, var2.width, var2.height);
  113.          this.offGraphics.setColor(Color.black);
  114.          this.offGraphics.drawImage(this.images[this.frameNumber % 10], 0, 0, this);
  115.          var1.drawImage(this.offImage, 0, 0, this);
  116.       }
  117.    }
  118. }
  119.