FileDocCategorySizeDatePackage
Animator.javaAPI DocSun JDK 1.5.0 Example29119Sat Jan 08 15:09:02 GMT 2005None

Animator

public class Animator extends Applet implements MouseListener, Runnable
An applet that plays a sequence of images, as a loop or a one-shot. Can have a soundtrack and/or sound effects tied to individual frames. See the Animator home page for details and updates.
author
Herb Jellinek
version
1.13, 07/26/04

Fields Summary
int
appWidth
int
appHeight
Thread
engine
boolean
userPause
boolean
loaded
boolean
error
Animation
animation
String
hrefTarget
URL
hrefURL
static final String
sourceLocation
static final String
userInstructions
static final int
STARTUP_ID
static final int
BACKGROUND_ID
static final int
ANIMATION_ID
Constructors Summary
Methods Summary
voidclearLoadingMessage()

	showStatus("");
    
private java.awt.ColordecodeColor(java.lang.String s)

	int val = 0;
	try {
	    if (s.startsWith("0x")) {
		val = Integer.parseInt(s.substring(2), 16);
	    } else if (s.startsWith("#")) {
		val = Integer.parseInt(s.substring(1), 16);
	    } else if (s.startsWith("0") && s.length() > 1) {
		val = Integer.parseInt(s.substring(1), 8);
	    } else {
		val = Integer.parseInt(s, 10);
	    }
	    return new Color(val);
	} catch (NumberFormatException e) {
	    return null;
	}
    
public voiddestroy()

        removeMouseListener(this);
    
public java.lang.StringgetAppletInfo()
Applet info.


           
       
	return "Animator v1.10 (02/05/97), by Herb Jellinek";
    
public java.lang.StringgetParam(java.lang.String key)
Local version of getParameter for debugging purposes.

	String result = getParameter(key);
	return result;
    
public java.lang.String[][]getParameterInfo()
Parameter info.

	String[][] info = {
	    {"imagesource", 	"URL", 	       "a directory"},
	    {"startup", 	"URL", 	       "image displayed at start-up"},
	    {"backgroundcolor", "int", "background color (24-bit RGB number)"},
	    {"background", 	"URL", 	      "image displayed as background"},
	    {"startimage", 	"int", 	       "index of first image"},
	    {"endimage", 	"int", 	       "index of last image"},
	    {"namepattern",     "URL",         "generates indexed names"},
	    {"images",          "URLs",        "list of image indices"},
	    {"href",		"URL",	       "page to visit on mouse-click"},
	    {"target",		"name",	       "frame to put that page in"},
	    {"pause", 	        "int", 	       "global pause, milliseconds"},
	    {"pauses", 	        "ints",     "individual pauses, milliseconds"},
	    {"repeat", 	        "boolean",     "repeat? true or false"},
	    {"positions",       "coordinates", "path images will follow"},
	    {"soundsource",	"URL", 	       "audio directory"},
	    {"soundtrack",	"URL", 	       "background music"},
	    {"sounds",		"URLs",	       "list of audio samples"},
	};
	return info;
    
public voidhandleParams()
Get parameters and parse them

        try {
	    String param = getParam("IMAGESOURCE");
	    animation.imageSource = (param == null) ? getDocumentBase() :
                new URL(getDocumentBase(), param + "/");
	
	    String href = getParam("HREF");
	    if (href != null) {
		try {
		    hrefURL = new URL(getDocumentBase(), href);
		} catch (MalformedURLException e) {
		    showParseError(e);
		}
	    }

	    hrefTarget = getParam("TARGET");
	    if (hrefTarget == null)
		hrefTarget = "_top";
	    param = getParam("PAUSE");
            if (param != null)
                animation.setGlobalPause(Integer.parseInt(param));
	    param = getParam("REPEAT");
	    animation.repeat = (param == null) ? true :
                (param.equalsIgnoreCase("yes") ||
                 param.equalsIgnoreCase("true"));
	    int startImage = 1;
	    int endImage = 1;
	    param = getParam("ENDIMAGE");
	    if (param != null) {
		endImage = Integer.parseInt(param);
		param = getParam("STARTIMAGE");
		if (param != null) {
		    startImage = Integer.parseInt(param);
		}
                param = getParam("NAMEPATTERN");
                animation.prepareImageRange(startImage, endImage, param);
	    } else {
		param = getParam("STARTIMAGE");
		if (param != null) {
		    startImage = Integer.parseInt(param);
		    param = getParam("NAMEPATTERN");
		    animation.prepareImageRange(startImage, endImage, param);
		} else {
		    param = getParam("IMAGES");
		    if (param == null) {
			showStatus("No legal IMAGES, STARTIMAGE, or ENDIMAGE "+
				   "specified.");
			error = true;
			return;
		    } else {
			animation.parseImages(param, getParam("NAMEPATTERN"));
		    }
		}
	    }

	    param = getParam("BACKGROUND");
	    if (param != null)
		animation.backgroundImageURL = new URL(animation.imageSource,
                                                       param);
	    param = getParam("BACKGROUNDCOLOR");
	    if (param != null)
		animation.backgroundColor = decodeColor(param);
	    param = getParam("STARTUP");
	    if (param != null)
		animation.startUpImageURL = new URL(animation.imageSource, 
                                                    param);
	    param = getParam("SOUNDSOURCE");
	    animation.soundSource = (param == null) ? animation.imageSource :
                new URL(getDocumentBase(), param + "/");	
	    param = getParam("SOUNDS");
	    if (param != null)
		animation.parseSounds(param);
	    param = getParam("PAUSES");
	    if (param != null)
		animation.parseDurations(param);
	    param = getParam("POSITIONS");
	    if (param != null) 
		animation.parsePositions(param);
	    param = getParam("SOUNDTRACK");
	    if (param != null)
		animation.soundTrackURL = new URL(
                                          animation.soundSource, param);
	} catch (MalformedURLException e) {
	    showParseError(e);
	} catch (ParseException e) {
	    showParseError(e);
	}
    
public voidinit()
Initialize the applet. Get parameters.

        //animation.tracker = new MediaTracker(this);
	appWidth = getSize().width;
	appHeight = getSize().height;
        animation = new Animation(this);
        handleParams();
        animation.init();
	addMouseListener(this);
        Thread me = Thread.currentThread();
        me.setPriority(Thread.MIN_PRIORITY);
        userPause = false;
    
voidloadError(java.lang.String fileName, java.lang.String fileType)

	String errorMsg = "Animator: Couldn't load "+fileType+" "+
	    fileName;
	showStatus(errorMsg);
	System.err.println(errorMsg);
	error = true;
	repaint();
    
voidloadError(java.net.URL badURL, java.lang.String fileType)

	loadError(badURL.toExternalForm(), fileType);
    
public voidmouseClicked(java.awt.event.MouseEvent event)

        if ((hrefURL != null) &&
            ((event.getModifiers() & InputEvent.SHIFT_MASK) == 0)) 
        {
            // Note: currently ignored by appletviewer
            getAppletContext().showDocument(hrefURL, hrefTarget);
        }

        showStatus(getAppletInfo() + " -- " + userInstructions);
    
public voidmouseEntered(java.awt.event.MouseEvent event)

    
public voidmouseExited(java.awt.event.MouseEvent event)

    
public synchronized voidmousePressed(java.awt.event.MouseEvent event)
Pause the thread when the user clicks the mouse in the applet. If the thread has stopped (as in a non-repeat performance), restart it.

        event.consume();
        if ((event.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
            showDescription();
            return;
        } else if (hrefURL != null) {
            // Let mouseClicked handle this.
            return;
        } else if (loaded) {
            userPause = !userPause;
            if (!userPause) {
                animation.startPlaying();
                notifyAll();
            }
        }
    
public voidmouseReleased(java.awt.event.MouseEvent event)

    
public voidpaint(java.awt.Graphics g)
Paint the current frame

        if (error || ! loaded) {
            if (animation.startUpImage != null) {
		if (animation.tracker.checkID(STARTUP_ID)) {
		    if (animation.backgroundColor != null) {
			g.setColor(animation.backgroundColor);
			g.fillRect(0, 0, appWidth, appHeight);
		    }
		    g.drawImage(animation.startUpImage, 0, 0, this);
		}
	    } else {
		if ((animation.backgroundImage != null) &&
		     (animation.tracker.checkID(BACKGROUND_ID)))
			g.drawImage(animation.backgroundImage, 0, 0, this);
		else 
		    g.clearRect(0, 0, appWidth, appHeight);
	    }
        } else {
            animation.paint(g);
        }
    
public voidrun()
Run the animation. This method is called by class Thread.

see
java.lang.Thread

        Thread me = Thread.currentThread();

        if (animation == null) 
            return; 
        if (animation.frames == null)
            return;
        if ((appWidth <= 0) || (appHeight <= 0))
            return;

	try {
            animation.startPlaying();  

            while (engine == me) {
                // Get current frame and paint it, play its sound
                AnimationFrame thisFrame = (AnimationFrame) 
                               animation.frames.get(animation.currentFrame);
                repaint();
                if (thisFrame.sound != null)
                    thisFrame.sound.play();

                animation.currentFrame++;
                // Check if we are done
                if (animation.currentFrame >= animation.frames.size()) {
                    if (animation.repeat)
                        animation.currentFrame = 0;
                    else return;
                }

                // Pause for duration or longer if user paused
                try {        
                    Thread.sleep(thisFrame.duration);
                    synchronized(this) {
                        while (userPause) {
                            animation.stopPlaying();
                            wait();
                        }
                    }
                } catch (InterruptedException e) {
                }
            }
        } finally {
            synchronized(this) {
                if (engine == me)
                    animation.stopPlaying();
            }
        }
    
voidshowDescription()
Show a crude "About" box. Displays credits, errors (if any), and parameter values and documentation.

	DescriptionFrame description = new DescriptionFrame();	    
	description.tell("\t\t"+getAppletInfo()+"\n");
	description.tell("Updates, documentation at "+sourceLocation+"\n\n");
	description.tell("Document base: "+getDocumentBase()+"\n");
	description.tell("Code base: "+getCodeBase()+"\n\n");
	
	Object errors[] = animation.tracker.getErrorsAny();
	if (errors != null) {
	    description.tell("Applet image errors:\n");
	    for (int i = 0; i < errors.length; i++) {
		if (errors[i] instanceof Image) {
		    AnimationFrame frame = (AnimationFrame) 
                                              animation.frames.get(i);
                    URL url = frame.imageLocation;
		    if (url != null) {
			description.tell(" "+url+" not loaded\n");
		    }
		}
	    }
	    description.tell("\n");
	}
	if (animation.frames == null || animation.frames.size() == 0)
	    description.tell("\n** No images loaded **\n\n");
	description.tell("Applet parameters:\n");
	description.tell(" width = "+getParameter("WIDTH")+"\n");
	description.tell(" height = "+getParameter("HEIGHT")+"\n");
	String params[][] = getParameterInfo();
	for (int i = 0; i < params.length; i++) {
	    String name = params[i][0];
	    description.tell(" "+name+" = "+getParameter(name)+
			     "\t ["+params[i][2]+"]\n");
	}
	description.show();
    
voidshowParseError(java.lang.Exception e)

	String errorMsg = "Animator: Parse error: "+e;
	showStatus(errorMsg);
	System.err.println(errorMsg);
	error = true;
	repaint();
    
public voidstart()
Start the applet by forking an animation thread.

	engine = new Thread(this);
	engine.start();
	showStatus(getAppletInfo());
    
public synchronized voidstop()
Stop the insanity, um, applet.

        engine = null;
        animation.stopPlaying();
        if (userPause) {
            userPause = false;
            notify();
        }
    
voidtellLoadingMsg(java.net.URL url, java.lang.String fileType)

	tellLoadingMsg(url.toExternalForm(), fileType);
    
voidtellLoadingMsg(java.lang.String file, java.lang.String fileType)

	showStatus("Animator: loading "+fileType+" "+file);
    
public voidupdate(java.awt.Graphics g)
No need to clear anything; just paint.

	paint(g);