import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

/* 
 * Based on Arthur van Hoff's animation examples, this application
 * can serve as a template for all animation applications.
 */
public class ImageSequence extends Frame implements Runnable, MouseListener {
    int frameNumber = -1;
    int delay;
    Thread animatorThread;
    boolean frozen = false;

    Dimension offDimension;
    Image offImage;
    Graphics offGraphics;

    Image[] images;

    ImageSequence (int fps, String windowTitle) {
        super(windowTitle);
        delay = (fps > 0) ? (1000 / fps) : 100;

        //Load all the images.
        images = new Image[10];
        for (int i = 1; i <= 10; i++) {
            images[i-1] = Toolkit.getDefaultToolkit().getImage(
                          "./images/T"+i+".gif");
        }
        addMouseListener(this);
    }

    public void startAnimation() {
        //Create and start the animating thread.
        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 void stopAnimation() {
        //Stop the animating thread.
        animatorThread = null;
    }


    public void mouseClicked(MouseEvent e){
    }
  
    public void mouseEntered(MouseEvent e){
    }
  
    public void mouseExited(MouseEvent e){
    }
  
    public void mousePressed(MouseEvent e){
        if (frozen) {
            frozen = false;
            startAnimation();
        } else {
            frozen = true;
            stopAnimation();
        }
    }
  
    public void mouseReleased(MouseEvent e){
    }

    public void run() {
        //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();

        //This is the animation loop.
        while (Thread.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 void update(Graphics g){ 
        paint(g);
    }

    public void paint(Graphics g) {
        Dimension d = getSize();
        //Create the offscreen graphics context, if no good one exists.
        if ( (offGraphics == null)
          || (d.width != offDimension.width)
          || (d.height != offDimension.height) ) {
            offDimension = d;
            offImage = createImage(d.width, d.height);
            offGraphics = offImage.getGraphics();
        }
        //Erase the previous image.
        offGraphics.setColor(getBackground());
        offGraphics.fillRect(0, 0, d.width, d.height);
        offGraphics.setColor(Color.black);

        //Paint the frame into the image.
        if(frameNumber>=0)
           offGraphics.drawImage(images[frameNumber % 10], 0, 0, this);

        //Paint the image onto the screen.
        g.drawImage(offImage, 0, 0, this);
    }

    public static void main(String args[]) {
        ImageSequence animator = null;
        int fps = 10;

        // Get frames per second from the command line argument
        if (args.length > 0) {
            try {
                fps = Integer.parseInt(args[0]);
            } catch (Exception e) {}
            
        }
        animator = new ImageSequence(fps,"Animator");
        animator.setSize(200, 60);
        animator.show();
        animator.startAnimation();
    }

}