home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / animatedpanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.5 KB  |  151 lines

  1. // AnimatedPanel.java
  2. // 28.02.96
  3. //
  4. // an animation
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Image;
  9. import java.awt.Graphics;
  10. import java.awt.Event;
  11. import java.awt.MediaTracker;
  12.  
  13. /**
  14.  * An extension of IdPanel that animates a sequence of images.
  15.  */
  16. public class AnimatedPanel extends IdPanel implements Runnable {
  17.   private Thread animation;
  18.   private int beginDelay;
  19.   private int delay;
  20.   private Image frames[];
  21.   private int currentFrame;
  22.   private protected Image currentFrameImage;
  23.   private int frameCount;
  24.  
  25.   private MediaTracker tracker;
  26.  
  27.   /** 
  28.    * @param beginDelay The delay at the beginning of each animation loop.
  29.    * @param delay      The delay between frames.
  30.    */
  31.   public AnimatedPanel (String id, String statusText, int beginDelay,
  32.             int delay, Appletlike app) {
  33.     super (id, statusText, app);
  34.     this.beginDelay = beginDelay;
  35.     this.delay = delay;
  36.     tracker = new MediaTracker (this);
  37.   }
  38.  
  39.   /** Sets the animation frames to frames[]. */
  40.   public void setFrames (Image frames[]) {
  41.     this.frames = frames;
  42.     if (frames != null) {
  43.       frameCount = frames.length;
  44.       for (int i = 0; i < frameCount; i++)
  45.     tracker.addImage (frames[i], i);
  46.     } else {
  47.       frameCount = 0;
  48.       currentFrameImage = null;
  49.       repaint ();
  50.     }
  51.   }
  52.  
  53.   /**
  54.    * Sets the animation frames to the files beginning with framesPath,
  55.    * appended with numbers from 1 to frameCount and '.gif'.
  56.    */
  57.   public void setFrames (String framesPath, int frameCount) {
  58.     this.frameCount = frameCount;
  59.     frames = new Image[frameCount];
  60.     
  61.     for (int i = 1; i <= frameCount; i++) {
  62.       frames[i-1] = app.getImage (framesPath + i + ".gif", i);
  63.       tracker.addImage (frames[i-1], i-1);
  64.     }
  65.   }
  66.  
  67.   /** Resets the animation and starts it. */
  68.   public void start () {
  69.     if (frameCount > 0) {
  70.       currentFrame = 0;
  71.       currentFrameImage = frames[0];
  72.       repaint ();
  73.       if (animation == null) {
  74.     animation = new Thread (this);
  75.     animation.start ();
  76.       }
  77.     }
  78.   }
  79.  
  80.   /** Stops the animation.  Duh. */
  81.   public void stop () {
  82.     if (animation != null && animation.isAlive ())
  83.       animation.stop ();
  84.     animation = null;
  85.   }
  86.  
  87.   /** When start starts, this is what it starts. */
  88.   public void run () {
  89.     try {
  90.       for (int i = 0; i < frameCount; i++) {
  91.     tracker.waitForID (i);
  92.     animation.yield ();
  93.       }
  94.     } catch (InterruptedException e) {
  95.     }
  96.  
  97.     while (animation != null)
  98.       this.animate ();
  99.   }
  100.  
  101.   /** Does one cycle through the animation. */
  102.   protected void animate () {
  103.     int realDelay;
  104.  
  105.     do {
  106.       repaint ();
  107.  
  108.       if (currentFrame == 0)
  109.     realDelay = delay + beginDelay;
  110.       else
  111.     realDelay = delay;
  112.     
  113.       try {
  114.     animation.sleep (realDelay);
  115.       } catch (InterruptedException e) {
  116.       }
  117.  
  118.       if ((currentFrame + 1) < frameCount) {
  119.     currentFrame++;
  120.       } else {
  121.     currentFrame = 0;
  122.       }
  123.       currentFrameImage = frames[currentFrame];
  124.     } while (currentFrame != 0);
  125.   }
  126.  
  127.   /** Draw the current frame. */
  128.   public void paint (Graphics g) {
  129.     if (currentFrameImage != null)
  130.       g.drawImage (currentFrameImage, 0, 0, this);
  131.   }
  132.  
  133.   /**
  134.    * If the user clicks and a next screen is defined, go there.  Otherwise
  135.    * stop the animation if it's running or start it if it isn't.
  136.    */
  137.   public boolean mouseDown (Event e, int x, int y) {
  138.     Object next = getNext ();
  139.     
  140.     if (next != null) {
  141.       return super.mouseDown (e, x, y);
  142.     } else if (animation != null) {
  143.       this.stop ();
  144.     } else {
  145.       this.start ();
  146.     }
  147.     return true;
  148.   }
  149.  
  150. }
  151.