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