FileDocCategorySizeDatePackage
MovingImageTimer.javaAPI DocExample5286Tue Dec 12 18:59:06 GMT 2000None

MovingImageTimer

public class MovingImageTimer extends JApplet implements ActionListener

Fields Summary
int
frameNumber
boolean
frozen
Timer
timer
AnimationPane
animationPane
static String
fgFile
static String
bgFile
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        //Advance animation frame.
        frameNumber++;

        //Display it.
        animationPane.repaint();
    
voidbuildUI(java.awt.Container container, java.awt.Image bgImage, java.awt.Image fgImage)

        int fps = 10;

        //How many milliseconds between frames?
        int delay = (fps > 0) ? (1000 / fps) : 100;

        //Set up a timer that calls this object's action handler.
        timer = new Timer(delay, this);
        timer.setInitialDelay(0);
        timer.setCoalesce(true);

        animationPane = new AnimationPane(bgImage, fgImage);
        container.add(animationPane, BorderLayout.CENTER);

        animationPane.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (frozen) {
                    frozen = false;
                    startAnimation();
                } else {
                    frozen = true;
                    stopAnimation();
                }
            }
       });
    
public voidinit()


    //Invoked only when run as an applet.
       
        //Get the images.
        Image bgImage = getImage(getCodeBase(), bgFile);
        Image fgImage = getImage(getCodeBase(), fgFile);
        buildUI(getContentPane(), bgImage, fgImage);
    
public static voidmain(java.lang.String[] args)

        Image bgImage = Toolkit.getDefaultToolkit().getImage(
                                MovingImageTimer.bgFile);
        Image fgImage = Toolkit.getDefaultToolkit().getImage(
                                MovingImageTimer.fgFile);

        JFrame f = new JFrame("MovingImageTimer");
        final MovingImageTimer controller = new MovingImageTimer();
        controller.buildUI(f.getContentPane(), bgImage, fgImage);

        f.addWindowListener(new WindowAdapter() {
            public void windowIconified(WindowEvent e) {
                controller.stopAnimation();
            }
            public void windowDeiconified(WindowEvent e) {
                controller.startAnimation();
            }
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setSize(new Dimension(500, 125));  
        f.setVisible(true);
        controller.startAnimation();
    
public voidstart()

        startAnimation();
    
public synchronized voidstartAnimation()

        if (frozen) { 
            //Do nothing.  The user has requested that we 
            //stop changing the image.
        } else {
            //Start animating!
            if (!timer.isRunning()) {
                timer.start();
            }
        }
    
public voidstop()

        stopAnimation();
    
public synchronized voidstopAnimation()

        //Stop the animating thread.
        if (timer.isRunning()) {
            timer.stop();
        }