home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap16 / Animation / Animation.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  4.7 KB  |  147 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.MediaTracker;
  8. import java.awt.Rectangle;
  9. import java.awt.image.ImageObserver;
  10.  
  11. public class Animation extends Applet implements Runnable {
  12.    Thread m_Animation;
  13.    private Graphics m_Graphics;
  14.    private Image[] m_Images;
  15.    private int m_nCurrImage;
  16.    private int m_nImgWidth;
  17.    private int m_nImgHeight;
  18.    private boolean m_fAllLoaded;
  19.    private int m_nSleepTime;
  20.    private int m_fps = 10;
  21.    private String m_imageFile = "";
  22.    private int m_numImages = 22;
  23.    private final String PARAM_fps = "fps";
  24.    private final String PARAM_imageFile = "imageFile";
  25.    private final String PARAM_numImages = "numImages";
  26.  
  27.    public void start() {
  28.       if (this.m_Animation == null) {
  29.          this.m_Animation = new Thread(this);
  30.          this.m_Animation.start();
  31.       }
  32.  
  33.    }
  34.  
  35.    public String[][] getParameterInfo() {
  36.       String[][] info = new String[][]{{"fps", "int", "Frame rate"}, {"imageFile", "String", "Name of the image seq"}, {"numImages", "int", "Number of images"}};
  37.       return info;
  38.    }
  39.  
  40.    public void stop() {
  41.       if (this.m_Animation != null) {
  42.          this.m_Animation.stop();
  43.          this.m_Animation = null;
  44.       }
  45.  
  46.    }
  47.  
  48.    private void displayImage(Graphics g) {
  49.       if (this.m_fAllLoaded) {
  50.          g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2, (ImageObserver)null);
  51.          g.setColor(Color.white);
  52.          g.drawString("Image #" + this.m_nCurrImage, 10, 40);
  53.       }
  54.    }
  55.  
  56.    public String getAppletInfo() {
  57.       return "Name: Animation\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
  58.    }
  59.  
  60.    public void run() {
  61.       this.m_nCurrImage = 0;
  62.       if (!this.m_fAllLoaded) {
  63.          ((Component)this).repaint();
  64.          this.m_Graphics = ((Component)this).getGraphics();
  65.          this.m_Images = new Image[this.m_numImages];
  66.          MediaTracker tracker = new MediaTracker(this);
  67.  
  68.          for(int i = 1; i <= this.m_numImages; ++i) {
  69.             String strImage = this.m_imageFile + (i < 10 ? "0" : "") + i + ".gif";
  70.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), strImage);
  71.             tracker.addImage(this.m_Images[i - 1], 0);
  72.          }
  73.  
  74.          try {
  75.             tracker.waitForAll();
  76.             this.m_fAllLoaded = !tracker.isErrorAny();
  77.          } catch (InterruptedException var5) {
  78.          }
  79.  
  80.          if (!this.m_fAllLoaded) {
  81.             this.stop();
  82.             this.m_Graphics.drawString("Error loading images!", 10, 40);
  83.             return;
  84.          }
  85.  
  86.          this.m_nImgWidth = this.m_Images[0].getWidth(this);
  87.          this.m_nImgHeight = this.m_Images[0].getHeight(this);
  88.       }
  89.  
  90.       ((Component)this).repaint();
  91.       String sSoundClip = new String(this.m_imageFile + ".au");
  92.       AudioClip ac = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), sSoundClip);
  93.       ac.loop();
  94.  
  95.       while(true) {
  96.          try {
  97.             this.displayImage(this.m_Graphics);
  98.             ++this.m_nCurrImage;
  99.             if (this.m_nCurrImage == this.m_numImages) {
  100.                this.m_nCurrImage = 0;
  101.             }
  102.  
  103.             Thread.sleep((long)this.m_nSleepTime);
  104.          } catch (InterruptedException var6) {
  105.             this.stop();
  106.          }
  107.       }
  108.    }
  109.  
  110.    public void destroy() {
  111.    }
  112.  
  113.    public void init() {
  114.       String param = ((Applet)this).getParameter("fps");
  115.       if (param != null) {
  116.          this.m_fps = Integer.parseInt(param);
  117.       }
  118.  
  119.       param = ((Applet)this).getParameter("imageFile");
  120.       if (param != null) {
  121.          this.m_imageFile = param;
  122.       }
  123.  
  124.       param = ((Applet)this).getParameter("numImages");
  125.       if (param != null) {
  126.          this.m_numImages = Integer.parseInt(param);
  127.       }
  128.  
  129.       ((Applet)this).resize(320, 240);
  130.       if (this.m_fps != 0) {
  131.          this.m_nSleepTime = 1000 / this.m_fps;
  132.       }
  133.  
  134.    }
  135.  
  136.    public void paint(Graphics g) {
  137.       if (this.m_fAllLoaded) {
  138.          Rectangle r = g.getClipRect();
  139.          g.clearRect(r.x, r.y, r.width, r.height);
  140.          this.displayImage(g);
  141.       } else {
  142.          g.drawString("Loading images...", 10, 20);
  143.       }
  144.  
  145.    }
  146. }
  147.