/**************************************************************************** Copyright (C) 1996-1997 Intel Corporation All Rights Reserved A royalty-free copyright license to copy, modify and compile this source code and to distribute it in your own products in source and binary forms is hereby granted, so long as this copyright notice and this copyright license appear in all source code copies. No other rights or licenses are granted. The Intel name or other trademarks may not be used to endorse or promote products using this header file without Intel's prior written permission. THIS SOURCE CODE IS PROVIDED "AS IS" WITH NO WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE. Intel disclaims all liability, including liability for infringement of any proprietary rights, relating to use of this source code. ****************************************************************************/ package animTutorial; import java.applet.*; import java.awt.*; import java.awt.image.PixelGrabber; import anim.*; /** * AnimatingSprites. Demonstrates animating a StaticDataSprite. * Displays the a street scene and an Elvis sprite. * Button clicks can adjust Elvis current image or start animation thread * that constantly adjusts current image. */ public class AnimatingSprites extends Applet implements Runnable { // surface size static final int SURFACE_WIDTH = 500; static final int SURFACE_HEIGHT = 480; // initial sprite positions static final int SKY_X = 0; static final int SKY_Y = 0; static final int SKY_Z = 500; static final int MIDDLE_X = -120; static final int MIDDLE_Y = 0; static final int MIDDLE_Z = 400; static final int FLOOR_X = -170; static final int FLOOR_Y = 405; static final int FLOOR_Z = 300; static final int ELVIS_X = 120; static final int ELVIS_Y = 165; static final int ELVIS_Z = 200; // file locations for sprite images static final String SKY_FILE = "../images/Sky.gif"; static final String MIDDLE_FILE = "../images/Middle.gif"; static final String FLOOR_FILE = "../images/Floor.gif"; // Elvis animation sequence consists of four images static final String ELVIS_FILE[] = { "../images/Ewave1.gif", "../images/Ewave2.gif", "../images/Ewave3.gif", "../images/Ewave4.gif"}; // number of frames in animation sequence static final int NUMFRAMES_WAVE = 4; // size of Elvis image, in pixels static final int ELVIS_WIDTH = 264; static final int ELVIS_HEIGHT = 278; // StaticDataSprites for the applet. Every visible object in your // display is a sprite. StaticDataSprite Sky; StaticDataSprite Middle; StaticDataSprite Floor; StaticDataSprite Elvis; // animcanvas AnimCanvas canvas; // adjust current image and start and stop animation buttons Button AdjCurImageButton; Button StartButton; Button StopButton; // adjust animation speed controls Button FasterB; Button SlowerB; // animator thread Thread animator; // animation delay variables int MAX_DELAY = 20; int INIT_ANIM_DELAY = MAX_DELAY/2; int ANIM_DELAY_INC = 1; int count = 0; int animDelay = INIT_ANIM_DELAY; /** * Perform necessary initialization. */ public void init() { System.out.println(getAppletInfo()); System.out.println("Initializing applet"); // setup gui components // set borderlayout manager setLayout(new BorderLayout()); // add the AnimCanvas to the applet canvas = new AnimCanvas(SURFACE_WIDTH, SURFACE_HEIGHT); add("Center", canvas); // set up container for buttons Panel ControlPanel = new Panel(); ControlPanel.setLayout(new GridLayout(2,1,0,0)); Panel ButtonPanel = new Panel(); ButtonPanel.setLayout(new FlowLayout()); Panel AnimSpeedPanel = new Panel(); AnimSpeedPanel.setLayout(new FlowLayout()); // create button panel AdjCurImageButton = new Button("Adjust Current Image"); StartButton = new Button("Start Animation"); StopButton = new Button("Stop Animation"); StopButton.disable(); // animation not yet started ButtonPanel.add(AdjCurImageButton); ButtonPanel.add(StartButton); ButtonPanel.add(StopButton); // create anim speed panel FasterB = new Button("Slower"); SlowerB = new Button("Faster"); FasterB.disable(); SlowerB.disable(); AnimSpeedPanel.add(SlowerB); AnimSpeedPanel.add(FasterB); ControlPanel.add(ButtonPanel); ControlPanel.add(AnimSpeedPanel); add("South", ControlPanel); // initialize background sprites InitBackgroundSprites(); // initialize Elvis InitElvis(); System.out.println("Initializing applet: DONE"); } /** * Applet start method. */ public void start() { System.out.println("Starting the applet"); } /** * Applet stop method. */ public void stop() { System.out.println("Stopping the applet"); // stop animator thread and enable/disable start/stop buttons, if necessary if (StopButton.isEnabled()) { animator.stop(); StopButton.disable(); StartButton.enable(); AdjCurImageButton.enable(); } } /** * Applet destroy method. */ public void destroy() { System.out.println("Destroying the applet!"); } /** * Return applet info. * @return Applet info */ public String getAppletInfo() { return "AnimatingSprites v.1.2"; } /** * Event Handler for button clicks. Adjust current image or start/stop animation. * @param evt Event description * @param arg String on the face of the button */ public boolean action(Event evt, Object arg) { if (arg.equals("Adjust Current Image")) { // adjust current image by 1 Elvis.adjustCurrentImageNumber(1); // repaint AnimCanvas to show new image canvas.repaint(); } else if (arg.equals("Start Animation")) { animator = new Thread(this); animator.start(); StartButton.disable(); AdjCurImageButton.disable(); StopButton.enable(); FasterB.enable(); SlowerB.enable(); } else if (arg.equals("Stop Animation")) { animator.stop(); StartButton.enable(); AdjCurImageButton.enable(); StopButton.disable(); FasterB.disable(); SlowerB.disable(); } else if (arg.equals("Faster")) { int animDelayTmp = animDelay; animDelayTmp -= ANIM_DELAY_INC; setAnimDelay(animDelayTmp); } else if (arg.equals("Slower")) { int animDelayTmp = animDelay; animDelayTmp += ANIM_DELAY_INC; setAnimDelay(animDelayTmp); } else // action not handled so return false return false; // return true to show that action was handled return true; } private void setAnimDelay(int delay) { if (delay == 0) animDelay = 1; else if (delay > MAX_DELAY) animDelay = MAX_DELAY; else animDelay = delay; } /** * Run method for animator thread. This method is called when * animator.start() is called. This method keeps adjusting the * current image of the Elvis Sprite. */ public void run() { while( true ) { // adjust current image by 1 if ((++count % animDelay) == 0) { Elvis.adjustCurrentImageNumber(1); count = 0; } // repaint AnimCanvas to show new image canvas.repaint(); } } /** * Initialize background sprites: * Create awt.Image using getImage * Create SingleImage * Set transparancy in SingleImage (if necessary) * Create Sprite * Set Sprite's destination surface * Position Sprite */ private void InitBackgroundSprites() { // awt.Image for SingleImage Image image; // SingleImage for sprite SingleImage spriteSource = null; // Initialize Sky sprite // create awt.Image image = getImage(getDocumentBase(), SKY_FILE); try { // instantiate SingleImage spriteSource = new SingleImage(image); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // instantiate the sprite Sky = new StaticDataSprite(); Sky.setData(spriteSource); // set sprite to surface Sky.setDestination(canvas.getSurface()); // position sprite Sky.setPosition(new Point3D(SKY_X, SKY_Y, SKY_Z)); // Initialize Middle sprite // create awt.Image image = getImage(getDocumentBase(), MIDDLE_FILE); try { // instantiate SingleImage spriteSource = new SingleImage(image); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // Set transparency for SingleImage setTransparency(image, spriteSource); // instantiate the sprite Middle = new StaticDataSprite(); Middle.setData(spriteSource); // set sprite to surface Middle.setDestination(canvas.getSurface()); // position sprite Middle.setPosition(new Point3D(MIDDLE_X, MIDDLE_Y, MIDDLE_Z)); // Initialize Floor sprite // create awt.Image image = getImage(getDocumentBase(), FLOOR_FILE); try { // instantiate SingleImage spriteSource = new SingleImage(image); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // Set transparency for SingleImage setTransparency(image, spriteSource); // instantiate the sprite Floor = new StaticDataSprite(); Floor.setData(spriteSource); // set sprite to surface Floor.setDestination(canvas.getSurface()); // position sprite Floor.setPosition(new Point3D(FLOOR_X, FLOOR_Y, FLOOR_Z)); } /** * Initialize Elvis: * Create ImageSequence * for each SingleImage in ImageSequence * Create awt.Image using getImage * Create SingleImage * Set transparancy in SingleImage (if necessary) * Add SingleImage to ImageSequence * Create Sprite * Set Sprite's destination surface * Position Sprite */ private void InitElvis() { // awt.Image for SingleImage Image image; // SingleImage for ImageSequence SingleImage spriteSource = null; // ImageSequence for Elvis Sprite ImageSequence seq = null; // create ImageSequence seq = new ImageSequence(NUMFRAMES_WAVE, ELVIS_WIDTH, ELVIS_HEIGHT); // create SingleImages and add to ImageSequence for(int seqnum = 0; seqnum < NUMFRAMES_WAVE; seqnum++) { // create awt.Image image = getImage(getDocumentBase(), ELVIS_FILE[seqnum]); try { // create SingleImage spriteSource = new SingleImage(image); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // set transparency for SingleImage setTransparency(image, spriteSource); // add SingleImage to ImageSequence seq.setElementAt(spriteSource, seqnum); } // instantiate the sprite Elvis = new StaticDataSprite(); Elvis.setData(seq); // set sprite to surface Elvis.setDestination(canvas.getSurface()); // position sprite Elvis.setPosition(new Point3D(ELVIS_X, ELVIS_Y, ELVIS_Z)); } /** * Set transparency color of spriteSource to the color of the * first pixel in spriteImage. * @param spriteImage Retrieve color of first pixel in this image * @param spriteSource Set transparency color of this singleImage */ private static void setTransparency(Image spriteImage, SingleImage spriteSource) { // first pixel of spriteImage int pixel[] = new int[1]; // PixelGrabber that retrives first pixel PixelGrabber pg = new PixelGrabber(spriteImage, // image 0, // x 0, // y 1, // width 1, // height pixel, // array to hold the pixels 0, // offset into array of pixel(0,0) 1 // pitch ); // retrieve pixel try { pg.grabPixels(); } catch (InterruptedException e) { throw new AnimInternalError("PixelGrabber interrupted"); } // determine pixel color Color color = new Color(pixel[0]); // set spritesource transparency color spriteSource.setTransparencyColor(color); } } // applet AnimatingSprites