/** * @JDAnimate.java * @version 1.0, 28 Sep 1997 * @author: John Donohue * * This is a simple animation applet. You specify a list of files that contain * each frame of animation, how long to display each frame for, and a "*.au" * file that has the soundtrack. The first frame is displayed until the user * clicks on the applet, it then cycles through the rest of the frames and retuns * to the first frame. * * Example of APPLET code: * * * * * * * * Parameters: * * frameNames * Is a list of the image files names separated by commas. * * frameRate * Is the rate to play the frames in milliseconds (1000 = 1 second). * * soundTrack * Is the *.au file to play during animation. If you don't have a soundTrack * specify an emptey soundTrack like so: * * */ import java.awt.*; import java.applet.*; import java.util.*; import java.net.*; /** * The Applet. */ public class JDAnimate extends Applet implements Runnable { MediaTracker mt; private static AudioClip evilLaughSound; private Image frameImage; private Image bufferImage; private Graphics bufferGraphics; private Thread animate; private Vector framesVector; private Enumeration framesEnum; private boolean runAnimation; private int frameRate; private String soundTrack; /** * The Applet. */ public void init() { String frameNames; StringTokenizer t; String backgroundColor; String foregroundColor; evilLaughSound = null; bufferImage = createImage(bounds().width, bounds().height); bufferGraphics = bufferImage.getGraphics(); super.init(); frameNames = getParameter("frameNames"); soundTrack = getParameter("soundTrack"); frameRate = ( Integer.valueOf(getParameter("frameRate")) ).intValue(); if ( !soundTrack.equals("") ) evilLaughSound = getAudioClip( getDocumentBase(), soundTrack); framesVector = new Vector(99); runAnimation = false; frameNames = getParameter("frameNames"); t = new StringTokenizer(frameNames, ","); while(t.hasMoreTokens() ) { framesVector.addElement( getPicImage(t.nextToken()) ); } } public void start() { initAnimation(); if (animate == null) { animate = new Thread (this); animate.start(); } } /** * initAnimation */ public void initAnimation() { framesEnum = framesVector.elements(); frameImage = (Image)framesEnum.nextElement(); bufferGraphics.drawImage(frameImage,0,0,this); repaint(); } /** * Run */ public void run() { for (;;) { if ( runAnimation ) { if ( framesEnum.hasMoreElements() ) { frameImage = (Image)framesEnum.nextElement(); bufferGraphics.drawImage(frameImage,0,0,this); } else { runAnimation = false; initAnimation(); } } try { Thread.sleep(frameRate); } //We repaint the screen every 15 micro seconds catch(InterruptedException e) { return; }; repaint(); } } /** * stop */ public void stop() { if (animate != null) { animate.stop(); animate=null; } } /** * update */ public void update(Graphics g) { paint(g); } /** * paint */ public void paint(Graphics g) { g.drawImage(bufferImage, 0, 0, this); } /** * */ public boolean mouseDown(Event evt, int x, int y) { if (runAnimation) initAnimation(); else runAnimation = true; if ( evilLaughSound != null) evilLaughSound.play(); return false; } //*************** getPicImage ************************************** // Use MediaTracker to insure the images are fully loaded before // we try to use them private Image getPicImage(String imageFileName) { Image imgWork = null; mt = new MediaTracker(this); try { imgWork = getImage(getDocumentBase(),imageFileName); } catch(Exception e1) { } mt.addImage(imgWork, 0); try { showStatus("Loading image " + imageFileName ); mt.checkID(0, true); mt.waitForID(0); } catch(InterruptedException e2) { } return imgWork; } }