package toy.dialog; import java.awt.*; import toy.image.*; /** * * @author Brian Tsang * @version 7.0 */ public class ToyAnimatedIcon extends Component implements Runnable { //////////////////////////////////////////////////////////////////////////// // Variables private ToyImageManager imageManager; private boolean stopping; private Thread runner; private int animationCtr; //////////////////////////////////////////////////////////////////////////// // The Constructor public ToyAnimatedIcon(ToyImageManager newImageManager) { imageManager = newImageManager; } //////////////////////////////////////////////////////////////////////////// // The Finalizer public void finalize() { runner = null; } //////////////////////////////////////////////////////////////////////////// // Describe our new Component public boolean isFocusTraversable() { return false; } public boolean isDoubleBuffered() { return false; } public boolean isLightweight() { return true; } //////////////////////////////////////////////////////////////////////////// // The get and set functions public boolean hasFocus() { return false; } public void setEnabled(boolean b) { super.setEnabled(b); repaint(); } public void setVisible(boolean b) { if (b == isVisible()) return; super.setVisible(b); if (getParent() != null) { if (b) repaint(); else getParent().repaint( getBounds().x, getBounds().y, getBounds().width, getBounds().height ); } } public Dimension getMinimumSize() { return new Dimension( ToyImageManager.ANIMATION_WIDTH, ToyImageManager.ANIMATION_HEIGHT ); } public Dimension getPreferredSize() { return new Dimension( ToyImageManager.ANIMATION_WIDTH, ToyImageManager.ANIMATION_HEIGHT ); } public Dimension getMaximumSize() { return new Dimension( ToyImageManager.ANIMATION_WIDTH, ToyImageManager.ANIMATION_HEIGHT ); } //////////////////////////////////////////////////////////////////////////// // Overriding the painting functions public void repaint() { paint(getGraphics()); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (getParent() == null || !isVisible() || g == null) return; int animationX = ToyImageManager.ANIMATION_WIDTH * (animationCtr % ToyImageManager.ANIMATION_COLUMNS); int animationY = ToyImageManager.ANIMATION_HEIGHT * (animationCtr / ToyImageManager.ANIMATION_COLUMNS); g.drawImage( imageManager.getImage(ToyImageManager.POPUP_ANIMATION), -animationX, -animationY, this ); } //////////////////////////////////////////////////////////////////////////// // start() begins the animation process public void start() { if (runner == null) { //Our animation thread runner = new Thread(this); runner.start(); } stopping = false; } //////////////////////////////////////////////////////////////////////////// // stop() brings the animation process to a halt (the animation goes all // the way until its starting point before the Thread stops public void stop() { stopping = true; } //////////////////////////////////////////////////////////////////////////// // Implement Runnable public void run() { Thread thisThread = Thread.currentThread(); while (runner == thisThread) { try { //animate animationCtr++; if (animationCtr >= ToyImageManager.ANIMATION_FRAMES) animationCtr = 0; repaint(); //the animation is at a point where we can stop if (stopping && animationCtr == 0) runner = null; } catch (Exception e) { //whatever } try { //pause for a number of milliseconds if (runner == thisThread) Thread.sleep(100); } catch (Exception e) { //whatever } } } }