FileDocCategorySizeDatePackage
Animator2.javaAPI DocExample3296Thu Aug 08 12:38:10 BST 1996None

Animator2

public class Animator2 extends Applet implements Runnable

Fields Summary
protected Image[]
images
protected int
num_images
protected int
current_image
protected MediaTracker
tracker
private Thread
animator_thread
Constructors Summary
Methods Summary
public voidinit()

        String basename = this.getParameter("basename");
        try { num_images = Integer.parseInt(this.getParameter("num_images")); }
        catch (NumberFormatException e) { num_images = 0; }
        
        // getImage() creates an Image object from a URL specification,
        // but it doesn't actually load the images; that is done 
        // asynchronously.  Store all the images in a MediaTracker
        // so we can wait until they have all loaded (in run()).
        tracker = new MediaTracker(this);
        images = new Image[num_images];
        for(int i = 0; i < num_images; i++) {
            images[i] = this.getImage(this.getDocumentBase(), basename + i);
            tracker.addImage(images[i], i);
        }
    
public voidrun()

        // First, force all the images to be loaded, and wait until
        // they have all loaded completely.
        for (int i = 0; i < num_images; i++) {
            this.showStatus("Loading image: " + i);
            // The argument is the same one we passed to addImage()
            try { tracker.waitForID(i); } catch (InterruptedException e) { ; }
            // Check for errors loading it.
            if (tracker.isErrorID(i)) {
                this.showStatus("Error loading image " + i + "; quitting.");
                return;
            }
        }
        this.showStatus("Loading images: done.");
        
        // Now do the animation
        while(true) {
            if (++current_image >= images.length) current_image = 0;
            this.getGraphics().drawImage(images[current_image], 0, 0, this);
            this.getToolkit().sync();  // Force it to be drawn *now*.
            try { Thread.sleep(200); } catch (InterruptedException e) { ; }
        }
    
public voidstart()

       
        if (animator_thread == null) {
            animator_thread = new Thread(this);
            animator_thread.start();
        }
    
public voidstop()

        if ((animator_thread != null) && animator_thread.isAlive()) 
            animator_thread.stop();
        animator_thread = null;