IndexImageAnimator.java
/*
* "COM.bensoft.widgets" Copyright (C) by Michael Benson - 7/27/97
*
* "ImageAnimator" - rotates through a series of images for
* simple animation.
*/
package COM.bensoft.widgets;
import java.awt.*;
import java.applet.Applet;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* ImageAnimator implements an ImageCanvas which rotates through
* a sequence of images for simple animation.
*
* @author Michael Benson
*/
public class ImageAnimator extends ImageCanvas implements Runnable
{
private Thread _animator_thread = null; // Animation thread.
private Vector _images;
private int _numImages;
private int _curImage;
private int _sleep;
/**
* ImageAnimator constructor. The image file name is the base name
* of a "family" of files named like this:
*
* codebase/fileN.gif
*
* In the above case, the image file name is "file" and the family of
* file names would be "file0.gif", "file1.gif", "file2.gif", etc.
*
* @param applet the parent applet (used for code base).
* @param name base name of image files.
* @param width width of the image.
* @param height height of the image.
* @param numImages number of images in the family.
* @param sleep number of milliseconds to sleep between images.
*/
public ImageAnimator (Applet applet, String name, int width, int height, int numImages, int sleep)
{
super(applet, width, height);
_fileName = name;
_numImages = numImages;
_images = new Vector();
_sleep = sleep;
// Initialize the images:
for (int i = 0; i < _numImages; i++) {
_images.addElement(_applet.getImage(_applet.getCodeBase(), _fileName + i + ".gif"));
}
_image = (Image)(_images.elementAt(0));
_curImage = 0;
}
/**
* Start the animation thread.
*/
public void start()
{
if (_animator_thread == null) {
_animator_thread = new Thread(this);
_animator_thread.setPriority(Thread.MAX_PRIORITY);
_animator_thread.start();
}
}
/**
* Stop the animation thread.
*/
public void stop()
{
if ((_animator_thread != null) && _animator_thread.isAlive()) {
_animator_thread.stop();
}
_animator_thread = null;
}
/**
* Run the animation thread until it is interruped.
*/
public void run()
{
while (true) {
if (++_curImage >= _numImages) _curImage = 0;
setImage((Image)(_images.elementAt(_curImage)));
try { Thread.sleep(_sleep); }
catch (InterruptedException e) {}
}
}
}