home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / Ms_vj / Samples / JavaNow / Chap16 / AnimationMT / AnimationMT.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  3.7 KB  |  115 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.awt.image.ImageObserver;
  7.  
  8. public class AnimationMT extends Applet implements Runnable {
  9.    Thread m_AnimationMT;
  10.    private int m_fps = 10;
  11.    private String m_imageFile = "LJN";
  12.    private int m_numImages = 22;
  13.    private final String PARAM_fps = "fps";
  14.    private final String PARAM_imageFile = "imageFile";
  15.    private final String PARAM_numImages = "numImages";
  16.    MediaTracker m_mediaTracker = new MediaTracker(this);
  17.    int m_nFrameNumber;
  18.    Image[] m_frame;
  19.  
  20.    public void start() {
  21.       if (this.m_AnimationMT == null) {
  22.          this.m_AnimationMT = new Thread(this);
  23.          this.m_AnimationMT.start();
  24.       }
  25.  
  26.    }
  27.  
  28.    public String[][] getParameterInfo() {
  29.       String[][] info = new String[][]{{"fps", "int", "Frame rate"}, {"imageFile", "String", "Name of file seq"}, {"numImages", "int", "Number of images"}};
  30.       return info;
  31.    }
  32.  
  33.    public void stop() {
  34.       if (this.m_AnimationMT != null) {
  35.          this.m_AnimationMT.stop();
  36.          this.m_AnimationMT = null;
  37.       }
  38.  
  39.    }
  40.  
  41.    public String getAppletInfo() {
  42.       return "Name: AnimationMT\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
  43.    }
  44.  
  45.    public void run() {
  46.       try {
  47.          this.m_nFrameNumber = 0;
  48.          ((Component)this).repaint();
  49.          this.m_mediaTracker.waitForAll();
  50.          ((Applet)this).resize(this.m_frame[0].getWidth((ImageObserver)null), this.m_frame[0].getHeight((ImageObserver)null));
  51.          int nDelay = 1000 / this.m_fps;
  52.  
  53.          while(true) {
  54.             ((Component)this).repaint();
  55.             Thread.sleep((long)nDelay);
  56.          }
  57.       } catch (InterruptedException var3) {
  58.          this.stop();
  59.       }
  60.    }
  61.  
  62.    public void destroy() {
  63.    }
  64.  
  65.    public void init() {
  66.       String param = ((Applet)this).getParameter("fps");
  67.       if (param != null) {
  68.          this.m_fps = Integer.parseInt(param);
  69.       }
  70.  
  71.       param = ((Applet)this).getParameter("imageFile");
  72.       if (param != null) {
  73.          this.m_imageFile = param;
  74.       }
  75.  
  76.       param = ((Applet)this).getParameter("numImages");
  77.       if (param != null) {
  78.          this.m_numImages = Integer.parseInt(param);
  79.       }
  80.  
  81.       ((Applet)this).resize(320, 240);
  82.       this.m_frame = new Image[this.m_numImages];
  83.  
  84.       for(int i = 0; i < this.m_numImages; ++i) {
  85.          int nImageNum = i + 1;
  86.          String sImageFile = this.m_imageFile + (nImageNum < 10 ? "0" : "") + nImageNum + ".gif";
  87.          this.m_frame[i] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), sImageFile);
  88.          this.m_mediaTracker.addImage(this.m_frame[i], 0);
  89.       }
  90.  
  91.    }
  92.  
  93.    public void paint(Graphics g) {
  94.       if (this.m_mediaTracker.checkAll()) {
  95.          if (this.m_mediaTracker.isErrorAny()) {
  96.             g.drawString("Error loading an image", 10, 20);
  97.             return;
  98.          }
  99.  
  100.          g.drawImage(this.m_frame[this.m_nFrameNumber], 0, 0, (ImageObserver)null);
  101.          ++this.m_nFrameNumber;
  102.          if (this.m_nFrameNumber >= this.m_numImages) {
  103.             this.m_nFrameNumber = 0;
  104.          }
  105.       } else {
  106.          g.drawString("Loading images...", 10, 20);
  107.       }
  108.  
  109.    }
  110.  
  111.    public void update(Graphics g) {
  112.       this.paint(g);
  113.    }
  114. }
  115.