home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Internet / Java / ui / drawing / example / ImageSequence.class (.txt) < prev    next >
Encoding:
Java Class File  |  1978-03-06  |  3.0 KB  |  105 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.  
  9. public class ImageSequence extends Applet implements Runnable {
  10.    int frameNumber = -1;
  11.    int delay;
  12.    Thread animatorThread;
  13.    boolean frozen = false;
  14.    Dimension offDimension;
  15.    Image offImage;
  16.    Graphics offGraphics;
  17.    Image[] images;
  18.  
  19.    public void init() {
  20.       int var2 = 10;
  21.       String var1 = ((Applet)this).getParameter("fps");
  22.  
  23.       try {
  24.          if (var1 != null) {
  25.             var2 = Integer.parseInt(var1);
  26.          }
  27.       } catch (Exception var4) {
  28.       }
  29.  
  30.       this.delay = var2 > 0 ? 1000 / var2 : 100;
  31.       this.images = new Image[10];
  32.  
  33.       for(int var3 = 1; var3 <= 10; ++var3) {
  34.          this.images[var3 - 1] = ((Applet)this).getImage(((Applet)this).getCodeBase(), "../../../images/duke/T" + var3 + ".gif");
  35.       }
  36.  
  37.    }
  38.  
  39.    public void start() {
  40.       if (!this.frozen) {
  41.          if (this.animatorThread == null) {
  42.             this.animatorThread = new Thread(this);
  43.          }
  44.  
  45.          this.animatorThread.start();
  46.       }
  47.  
  48.    }
  49.  
  50.    public void stop() {
  51.       this.animatorThread = null;
  52.       this.offGraphics = null;
  53.       this.offImage = null;
  54.    }
  55.  
  56.    public boolean mouseDown(Event var1, int var2, int var3) {
  57.       if (this.frozen) {
  58.          this.frozen = false;
  59.          this.start();
  60.       } else {
  61.          this.frozen = true;
  62.          this.animatorThread = null;
  63.       }
  64.  
  65.       return true;
  66.    }
  67.  
  68.    public void run() {
  69.       Thread.currentThread().setPriority(1);
  70.       long var1 = System.currentTimeMillis();
  71.  
  72.       while(Thread.currentThread() == this.animatorThread) {
  73.          ++this.frameNumber;
  74.          ((Component)this).repaint();
  75.  
  76.          try {
  77.             var1 += (long)this.delay;
  78.             Thread.sleep(Math.max(0L, var1 - System.currentTimeMillis()));
  79.          } catch (InterruptedException var3) {
  80.             return;
  81.          }
  82.       }
  83.  
  84.    }
  85.  
  86.    public void paint(Graphics var1) {
  87.       this.update(var1);
  88.    }
  89.  
  90.    public void update(Graphics var1) {
  91.       Dimension var2 = ((Component)this).size();
  92.       if (this.offGraphics == null || var2.width != this.offDimension.width || var2.height != this.offDimension.height) {
  93.          this.offDimension = var2;
  94.          this.offImage = ((Component)this).createImage(var2.width, var2.height);
  95.          this.offGraphics = this.offImage.getGraphics();
  96.       }
  97.  
  98.       this.offGraphics.setColor(((Component)this).getBackground());
  99.       this.offGraphics.fillRect(0, 0, var2.width, var2.height);
  100.       this.offGraphics.setColor(Color.black);
  101.       this.offGraphics.drawImage(this.images[this.frameNumber % 10], 0, 0, this);
  102.       var1.drawImage(this.offImage, 0, 0, this);
  103.    }
  104. }
  105.