FileDocCategorySizeDatePackage
AnimatorApplet.javaAPI DocExample2577Tue Dec 12 18:58:48 GMT 2000None

AnimatorApplet

public class AnimatorApplet extends Applet implements Runnable

Fields Summary
int
frameNumber
int
delay
Thread
animatorThread
boolean
frozen
Constructors Summary
Methods Summary
public voidinit()


       
        String str;
        int fps = 10;

        //How many milliseconds between frames?
        str = getParameter("fps");
        try {
            if (str != null) {
                fps = Integer.parseInt(str);
            }
        } catch (Exception e) {}
        delay = (fps > 0) ? (1000 / fps) : 100;

	addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (frozen) {
                    frozen = false;
                    start();
                } else {
                    frozen = true;
                    stop();
                }
            }
       });
    
public voidpaint(java.awt.Graphics g)

        g.drawString("Frame " + frameNumber, 0, 30);
    
public voidrun()

        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        //Remember the starting time.
        long startTime = System.currentTimeMillis();

        //Remember which thread we are.
        Thread currentThread = Thread.currentThread();

        //This is the animation loop.
        while (currentThread == animatorThread) {
            //Advance the animation frame.
            frameNumber++;

            //Display it.
            repaint();

            //Delay depending on how far we are behind.
            try {
                startTime += delay;
                Thread.sleep(Math.max(0, 
                             startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    
public voidstart()

        if (frozen) { 
            //Do nothing.  The user has requested that we 
            //stop changing the image.
        } else {
            //Start animating!
            if (animatorThread == null) {
                animatorThread = new Thread(this);
            }
            animatorThread.start();
        }
    
public voidstop()

        //Stop the animating thread.
        animatorThread = null;