/* * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.*; import java.applet.Applet; /* * This applet displays several images in a row. It prevents * flashing by double buffering. With the help of the MediaTracker, * it waits until all its images are fully loaded before drawing * them. */ public class MTImageSequence extends Applet implements Runnable { int frameNumber; int delay; Thread animatorThread; Dimension offDimension; Image offImage; Graphics offGraphics; Image images[]; MediaTracker tracker; public void init() { String str; // How many milliseconds between frames? str = getParameter("fps"); int fps = (str != null) ? Integer.parseInt(str) : 10; delay = (fps > 0) ? (1000 / fps) : 100; // Load all the images. images = new Image[10]; tracker = new MediaTracker(this); for (int i = 1; i <= 10; i++) { images[i-1] = getImage(getCodeBase(), "../../../images/duke/T"+i+".gif"); tracker.addImage(images[i-1], 0); } } public void start() { if (animatorThread == null) { animatorThread = new Thread(this); animatorThread.start(); } } public void stop() { animatorThread = null; offImage = null; offGraphics = null; } public boolean mouseDown(Event e, int x, int y) { if (animatorThread == null) { start(); } else { animatorThread = null; } return false; } public void run() { // Remember the starting time long startTime = System.currentTimeMillis(); while (Thread.currentThread() == animatorThread) { // Display the next frame of animation. repaint(); // Delay depending on how far we are behind. try { startTime += delay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break; } frameNumber++; } } // Paint the previous frame (if any). public void paint(Graphics g) { if (offImage != null) { g.drawImage(offImage, 0, 0, this); } } public void update(Graphics g) { Dimension d = size(); // 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 only if all images have arrived. if (tracker.statusID(0, true) == MediaTracker.COMPLETE) { offGraphics.drawImage(images[frameNumber % 10], 0, 0, this); } // Paint the image onto the screen. g.drawImage(offImage, 0, 0, this); } }