/**************************************************************************** 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.*; /** * CollidingSprites. * Displays two sprites, allows the user to move the sprites * right or left, and detects if the sprites have made contact */ public class CollidingSprites extends Applet { // surface size static final int SURFACE_WIDTH = 500; static final int SURFACE_HEIGHT = 480; // width in pixels to add/substract from the output view of each sprite static final int ELVIS_TRIM = 24; static final int WEEBLE_TRIM = 24; // movement constants static final int ELVIS_MOVEMENT = 5; static final int WEEBLE_MOVEMENT = 5; // Sprite width static final int ELVIS_WIDTH = 150; static final int WEEBLE_WIDTH = 150; // position constants static final int INIT_ELVIS_X = 92; static final int INIT_ELVIS_Y = 180; static final int INIT_ELVIS_Z = 100; static final int INIT_WEEBLE_X = 258; static final int INIT_WEEBLE_Y = 180; static final int INIT_WEEBLE_Z = 200; static final int COLLISION_SIGN_X = 133; static final int COLLISION_SIGN_Y = 120; static final int COLLISION_SIGN_Z = 300; static final int SKY_X = 0; static final int SKY_Y = 0; static final int SKY_Z = 600; static final int MIDDLE_X = -120; static final int MIDDLE_Y = 0; static final int MIDDLE_Z = 500; static final int FLOOR_X = -170; static final int FLOOR_Y = 405; static final int FLOOR_Z = 400; // file locations for sprite images static final String WEEBLE_FILE = "../images/weeble.gif"; static final String ELVIS_FILE = "../images/elvis.gif"; static final String COLLISION_FILE = "../images/collision.gif"; static final String SKY_FILE = "../images/Sky.gif"; static final String MIDDLE_FILE = "../images/Middle.gif"; static final String FLOOR_FILE = "../images/Floor.gif"; // StaticDataSprites for the applet. StaticDataSprite Weeble; StaticDataSprite Elvis; StaticDataSprite Collision; StaticDataSprite Sky; StaticDataSprite Middle; StaticDataSprite Floor; // animcanvas AnimCanvas canvas; // buttons Button eLeftB, eRightB, wLeftB, wRightB; // initialize position variables Point3D ElvisPos = new Point3D(INIT_ELVIS_X, INIT_ELVIS_Y, INIT_ELVIS_Z); Point3D WeeblePos = new Point3D(INIT_WEEBLE_X, INIT_WEEBLE_Y, INIT_WEEBLE_Z); Point3D CollPos = new Point3D(COLLISION_SIGN_X, COLLISION_SIGN_Y, COLLISION_SIGN_Z); // initialize rectangles for sprite collision detection Rectangle ElvisRect = new Rectangle(); Rectangle WeebleRect = new Rectangle(); /** * 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 GridLayout(1,2)); // set up sub-containers for buttons Panel eButtonPanel = new Panel(); Panel wButtonPanel = new Panel(); eButtonPanel.setLayout(new FlowLayout()); wButtonPanel.setLayout(new FlowLayout()); // create buttons eLeftB = new Button("Left"); eRightB = new Button("Right"); eButtonPanel.add(new Label("Elvis:")); eButtonPanel.add(eLeftB); eButtonPanel.add(eRightB); wLeftB = new Button("Left"); wRightB = new Button("Right"); wButtonPanel.add(new Label("Weeble")); wButtonPanel.add(wLeftB); wButtonPanel.add(wRightB); // add buttons ButtonPanel.add(eButtonPanel); ButtonPanel.add(wButtonPanel); add("South", ButtonPanel); // initialize backgrounds InitBackgroundSprites(); // initialize sprites InitSprites(); 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 "CollidingSprites 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) { // Elvis left if (evt.target == eLeftB) { Point3D checkPos = Elvis.getPosition(); if (checkPos.x <= 0) ElvisPos.x = 0; else ElvisPos.x -= ELVIS_MOVEMENT; Elvis.setPosition(ElvisPos); } // Elvis right else if (evt.target == eRightB) { Point3D checkPos = Elvis.getPosition(); if (checkPos.x >= (SURFACE_WIDTH - ELVIS_WIDTH)) ElvisPos.x = (SURFACE_WIDTH - ELVIS_WIDTH); else ElvisPos.x += ELVIS_MOVEMENT; Elvis.setPosition(ElvisPos); } // Weeble left else if (evt.target == wLeftB) { Point3D checkPos = Weeble.getPosition(); if (checkPos.x <= 0) WeeblePos.x = 0; else WeeblePos.x -= WEEBLE_MOVEMENT; Weeble.setPosition(WeeblePos); } // Weeble right else if (evt.target == wRightB) { Point3D checkPos = Weeble.getPosition(); if (checkPos.x >= (SURFACE_WIDTH - WEEBLE_WIDTH)) WeeblePos.x = (SURFACE_WIDTH - WEEBLE_WIDTH); else WeeblePos.x += WEEBLE_MOVEMENT; Weeble.setPosition(WeeblePos); } else return false; ElvisRect = Elvis.getOutputView(); WeebleRect = Weeble.getOutputView(); ElvisRect.x += ELVIS_TRIM; ElvisRect.width -= 2*ELVIS_TRIM; WeebleRect.x += WEEBLE_TRIM; WeebleRect.width -= 2*WEEBLE_TRIM; if (WeebleRect.intersects(ElvisRect)) Collision.setVisibility(true); else Collision.setVisibility(false); canvas.repaint(); return true; } /** * 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 InitSprites() { // awt.Image for SingleImage Image imageE; Image imageW; Image imageC; // SingleImage for Elvis Sprite SingleImage spriteSourceE = null; SingleImage spriteSourceW = null; SingleImage spriteSourceC = null; // create awt.Image imageE = getImage(getDocumentBase(), ELVIS_FILE); imageW = getImage(getDocumentBase(), WEEBLE_FILE); imageC = getImage(getDocumentBase(), COLLISION_FILE); try { // create SingleImage spriteSourceE = new SingleImage(imageE); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } try { // create SingleImage spriteSourceW = new SingleImage(imageW); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } try { // create SingleImage spriteSourceC = new SingleImage(imageC); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // set transparency for SingleImage setTransparency(imageE, spriteSourceE); setTransparency(imageW, spriteSourceW); setTransparency(imageC, spriteSourceC); // instantiate the sprite Elvis = new StaticDataSprite(); Elvis.setData(spriteSourceE); Weeble = new StaticDataSprite(); Weeble.setData(spriteSourceW); Collision = new StaticDataSprite(); Collision.setData(spriteSourceC); // set sprite to surface Elvis.setDestination(canvas.getSurface()); Weeble.setDestination(canvas.getSurface()); Collision.setDestination(canvas.getSurface()); // position sprite Elvis.setPosition(ElvisPos); Weeble.setPosition(WeeblePos); Collision.setPosition(CollPos); Collision.setVisibility(false); } /** * 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)); } /** * 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 CollidingSprites