home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / dek17tau / classes / spt / gui / animatedimagebutton.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.7 KB  |  166 lines

  1.  
  2. /*
  3.  * AnimatedImageButton.java
  4.  *
  5.  * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
  6.  */
  7.  
  8. package spt.gui;
  9.  
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.Event;
  13. import java.util.Vector;
  14.  
  15. import spt.gui.ImageButton;
  16. import spt.gui.ImagePanel;
  17.  
  18.  
  19. /**
  20.  * A push button on which animations can be displayed. A text label
  21.  * may also be displayed.
  22.  *
  23.  * @author Shaun Terry
  24.  */
  25.  
  26. public class AnimatedImageButton extends ImageButton implements Runnable {
  27.  
  28.     private Thread me;
  29.     private Vector images = new Vector();
  30.     private Vector delays = new Vector();
  31.     private int currImage = 0;
  32.     private boolean repeat = true;
  33.     private boolean readyToAnimate = false;
  34.     private boolean animateOnEnter = false;
  35.  
  36.     /** Create a button. The animation sequence must be supplied
  37.      *    later.
  38.      */
  39.      public AnimatedImageButton() {
  40.         this(null);
  41.     }
  42.  
  43.     /** Create a button with a text label. The animation sequence
  44.      *    must be supplied later.
  45.      */
  46.      public AnimatedImageButton(String label) {
  47.         super(null, label);
  48.         start();
  49.     }
  50.  
  51.     /** Starts the animation. Generally used only after a call to stop().
  52.      * @see #startAnimation
  53.      */
  54.     public void start() {
  55.         if (!readyToAnimate) return;
  56.         me = new Thread(this);
  57.         // If animating on mouse enter only, set MAX_PRIO for best performance
  58.         if (animateOnEnter) me.setPriority(Thread.MAX_PRIORITY);
  59.         me.start();
  60.     }
  61.  
  62.     /** Stops the animation.
  63.      */
  64.     public void stop() {
  65.         if (me != null) me.stop();
  66.         me = null;
  67.     }
  68.  
  69.     /** The entry point of the animation thread.
  70.      */
  71.     public void run() {
  72.         while (true) {
  73.             setImage((Image) images.elementAt(currImage));
  74.             if (currImage == images.size()-1 && !repeat) {    
  75.                 currImage = 0; // In case they want to resart later
  76.                 stop();
  77.                 return;
  78.             }
  79.             else {
  80.                 try {
  81.                     int s = ((Integer) delays.elementAt(currImage)).intValue();
  82.                     Thread.sleep(s);
  83.                 } catch (InterruptedException e) {}
  84.                 finally {
  85.                     if (++currImage == images.size()) currImage = 0;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     // Should also do a sound loop here!!!!
  92.  
  93.     /** Sets whether the animation whould be started when the
  94.      *    pointer enters the button area.
  95.      */
  96.     public void setAnimateOnEnter(boolean b) { animateOnEnter = b; }
  97.  
  98.     /** Call this method to begin the animation after all the images
  99.      *    in the animation have been supplied.
  100.      * @see #addImage
  101.      */
  102.     // This is needed because all the images may not have been loaded
  103.     // by the time the thread starts
  104.     public void startAnimation() {
  105.         readyToAnimate = true;
  106.         setImage((Image) images.elementAt(0));
  107.         resize();
  108.         if (!animateOnEnter) start();
  109.     }
  110.  
  111.     /** Reset the animation back to the first frame.
  112.      */
  113.     public void resetAnimation() {
  114.         currImage = 0;
  115.         setImage((Image) images.elementAt(currImage));
  116.         repaint();
  117.     }
  118.  
  119.     /** Should the animation repeat or just run once?
  120.      */
  121.     public void setRepeat(boolean b) { repeat = b; }
  122.  
  123.     /** Add the next image to the animation with a default delay
  124.      *    after the frame.
  125.      */
  126.     public void addImage(Image i) {
  127.         addImage(i, 300);    // default is 200 ms
  128.     }
  129.  
  130.     /** Add the next image to the animation. During playback
  131.      *    the given number of milliseconds should be paused before the
  132.      *    next frame.
  133.      */
  134.     public void addImage(Image i, int delay) {
  135.         images.addElement(i);
  136.         delays.addElement(new Integer(delay));
  137.  
  138.     }
  139.  
  140.     /** Disable the button. The animation is stopped and a "disabled"
  141.      *    version of the current image is displayed.
  142.      */
  143.     public void disable() {
  144.         stop();
  145.         super.disable();
  146.     }
  147.  
  148.     public boolean mouseEnter(Event e, int x, int y) {
  149.         if (animateOnEnter) {
  150.             if (!readyToAnimate) startAnimation();
  151.             start();
  152.         }
  153.         super.mouseEnter(e, x, y);
  154.         return true;
  155.     }
  156.  
  157.     public boolean mouseExit(Event e, int x, int y) {
  158.         if (animateOnEnter) {
  159.             stop();
  160.             resetAnimation();
  161.         }
  162.         super.mouseExit(e, x, y);
  163.         return true;
  164.     }
  165. }
  166.