/**************************************************************************** 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.*; /** * MovingSprites. * Displays a street scene and an Elvis sprite. * Button clicks move Elvis and scroll background. */ public class MovingSprites extends Applet { // 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; // movement constants // number of pixels to move sprites by static final int ELVIS_MOVEMENT = 5; static final int FLOOR_MOVEMENT = 5; static final int MIDDLE_MOVEMENT = 3; // x coordinate of Floor when it has moved the maximum amount left static final int FLOOR_LEFT_BOUNDARY = -335; // x coordinate of Floor when it has moved the maximum amount right static final int FLOOR_RIGHT_BOUNDARY = -5; // x coordinate of Elvis when it has moved the maximum amount left static final int ELVIS_LEFT_BOUNDARY = -65; // x coordinate of Elvis when it has moved the maximum amount right static final int ELVIS_RIGHT_BOUNDARY = 310; // 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"; static final String ELVIS_FILE = "../images/Ewalk1.gif"; // 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; /** * 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 ButtonPanel = new Panel(); ButtonPanel.setLayout(new FlowLayout()); // add buttons ButtonPanel.add(new Button("Left")); ButtonPanel.add(new Button("Right")); add("South", ButtonPanel); // 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"); } /** * Applet destroy method. */ public void destroy() { System.out.println("Destroying the applet!"); } /** * Return applet info. * @return Applet info */ public String getAppletInfo() { return "MovingSprites v.1.0"; } /** * Event Handler for button clicks. Move Elvis and scroll background. * @param evt Event description * @param arg String on the face of the button */ public boolean action(Event evt, Object arg) { if (arg.equals("Left")) { // determine position of Elvis sprite Point3D ElvisPos = Elvis.getPosition(); // determine position of Floor sprite Point3D FloorPos = Floor.getPosition(); // determine position of Middle sprite Point3D MiddlePos = Middle.getPosition(); // if Elvis is at the left edge, scroll background // (if background is not at its left edge) if (ElvisPos.x <= ELVIS_LEFT_BOUNDARY) { if (FloorPos.x < FLOOR_RIGHT_BOUNDARY) { // Floor can still be moved to the right, so move // Floor and Middle // Set new position coordinates FloorPos.x = FloorPos.x + FLOOR_MOVEMENT; MiddlePos.x = MiddlePos.x + MIDDLE_MOVEMENT; // Set Sprite position Floor.setPosition(FloorPos); Middle.setPosition(MiddlePos); } } else { // Elvis is not at the left edge, so just move the Elvis sprite // Set new position coordinates ElvisPos.x = ElvisPos.x - ELVIS_MOVEMENT; // Set Sprite position Elvis.setPosition(ElvisPos); } } else if (arg.equals("Right")) { // determine position of Elvis sprite Point3D ElvisPos = Elvis.getPosition(); // determine position of Floor sprite Point3D FloorPos = Floor.getPosition(); // determine position of Middle sprite Point3D MiddlePos = Middle.getPosition(); // if Elvis is at the right edge, scroll background // (if background is not at its right edge) if (ElvisPos.x >= ELVIS_RIGHT_BOUNDARY) { if (FloorPos.x > FLOOR_LEFT_BOUNDARY) { // Floor can still be moved to the left, so move // Floor and Middle // Set new position coordinates FloorPos.x = FloorPos.x - FLOOR_MOVEMENT; MiddlePos.x = MiddlePos.x - MIDDLE_MOVEMENT; // Set Sprite position Floor.setPosition(FloorPos); Middle.setPosition(MiddlePos); } } else { // Elvis is not at the right edge, so just move the Elvis sprite // Set new position coordinates ElvisPos.x = ElvisPos.x + ELVIS_MOVEMENT; // Set Sprite position Elvis.setPosition(ElvisPos); } } else // action not handled so return false return false; // repaint AnimCanvas to show new image canvas.repaint(); // return true to show that action was handled return true; } /** * 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) * Create Sprite * Set Sprite's destination surface * Position Sprite */ private void InitElvis() { // awt.Image for SingleImage Image image; // SingleImage for Elvis Sprite SingleImage spriteSource = null; // create awt.Image image = getImage(getDocumentBase(), ELVIS_FILE); try { // create 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 Elvis = new StaticDataSprite(); Elvis.setData(spriteSource); // 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 MovingSprites