FileDocCategorySizeDatePackage
Juggler.javaAPI DocExample6952Sat Apr 23 22:35:42 BST 2005magicbeans.sunw.demo.juggler

Juggler

public class Juggler extends JComponent implements PropertyChangeListener, Runnable, DesignMode
A simple JavaBean demonstration class that displays an animation of Duke juggling a couple of coffee beans. The Juggler class is a good simple example of how to write readObject/writeObject serialization methods that restore transient state. In this case the transient state is an array of images and a Thread.

Fields Summary
private transient Image[]
images
private transient Thread
animationThread
private int
rate
private transient int
loop
private boolean
stopped
private boolean
debug
private boolean
dmode
Constructors Summary
public Juggler()

    
      
        initialize();
        setJuggling(true);
    
Methods Summary
public intgetAnimationRate()

        return rate;
    
public java.awt.DimensiongetMinimumSize()

        return new Dimension(144, 125);
    
public java.awt.DimensiongetPreferredSize()

        return getMinimumSize();
    
private voidinitialize()
Initialize the Juggler .

        // Load the image resources:
        images = new Image[5];
        for (int i = 0; i < 5; i++) {
            String imageName = "Juggler" + i + ".gif";
            images[i] = loadImage(imageName);
            if (images[i] == null) {
                System.err.println("Couldn't load image " + imageName);
                return;
            }
        }
    
public booleanisDesignTime()

        return dmode;
    
public booleanisJuggling()
Returns false if the Juggler is stopped, true otherwise.

        return !stopped;
    
private java.awt.ImageloadImage(java.lang.String name)
This is an internal utility method to load GIF icons. It takes the name of a resource file associated with the current object's class-loader and loads a GIF image from that file.

return
a GIF image object. May be null if the load failed.

        try {
            java.net.URL url = getClass().getResource(name);
            
            ImageIcon icon = new ImageIcon(url);
            Image i = icon.getImage();
            return i;
        } catch (Exception ex) {
            return null;
        }
    
public voidpaintComponent(java.awt.Graphics g)
Draw the current frame.

        int index = (loop%4) + 1;
        // If the animation is stopped, show the startup image.
        if (stopped) {
            index = 0;
        }
        if (images == null || index >= images.length) {
            return;
        }
        Image img = images[index];
        if (img != null) {
            g.drawImage(img, 0, 0, this);
        }
    
public voidpropertyChange(java.beans.PropertyChangeEvent evt)

        if (evt.getPropertyName().equals("designMode")) {
            boolean dmode = (boolean)((Boolean)evt.getNewValue()).booleanValue();
            setDesignTime(dmode);
        }
    
private voidreadObject(java.io.ObjectInputStream s)

        s.defaultReadObject();
        initialize();
        if ( !stopped )
            start();
    
public voidrun()

        try {
            while(true) {
                // First wait until the animation is not stopped.
                synchronized (this) {
                    while (stopped || !isEnabled()) {
                        wait();
                    }
                }
                loop++;
                // Now draw the current frame.
                Graphics g = getGraphics();
                Image img = images[(loop % 4) + 1];
                if (g != null && img != null) {
                    g.drawImage(img, 0, 0, this);
                }
                Thread.sleep(rate);
            }
        } catch (InterruptedException e) {
        }
    
public voidsetAnimationRate(int x)

        rate = x;
    
public voidsetDesignTime(boolean dmode)

        this.dmode = dmode;
    
public synchronized voidsetEnabled(boolean x)
If false, suspend the animation thread.

        super.setEnabled(x);
        notify();
    
public voidsetJuggling(boolean b)

        if ( b )
            start();
        else
            stop();
    
public synchronized voidstart()
method: start the Juggler .

        startJuggling();
    
public voidstartJuggling(java.awt.event.ActionEvent x)
An event handling method that calls startJuggling. This method can be used to connect a Button or a MenuItem to the Juggler.

        startJuggling();
    
public synchronized voidstartJuggling()
Resume the animation thread if we're enabled.

see
#stopJuggling
see
#setEnabled

        if (images == null) {
            initialize();
        }
        if (animationThread == null) {
            animationThread = new Thread(this);
            animationThread.start();
        }
        stopped = false;
        notify();
    
public synchronized voidstop()
method: stop the Juggler .

        stopJuggling();
    
public voidstopJuggling(java.awt.event.ActionEvent x)
This method can be used to connect a Button or a MenuItem to the Juggler.stopJuggling method.

        stopJuggling();
    
public synchronized voidstopJuggling()
Suspend the animation thread if neccessary.

see
#startJuggling
see
#setEnabled

        stopped = true;
        loop = 0;
        // Draw the stopped frame.
        Graphics g = getGraphics();
        if (g == null || images == null) {
            return;
        }
        Image img = images[0];
        if (img != null) {
            g.drawImage(img, 0, 0, this);
        }