/**************************************************************************** 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.*; /** * UsingStaticDataSprites. Demonstrates usage of StaticDataSprite class. * Displays a street scene composed of three sprites and an Elvis sprite. * Transparency color of Elvis Sprite's SingleImage changes with button clicks. */ public class UsingStaticDataSprites extends Applet { // surface size static final int SURFACE_WIDTH = 500; static final int SURFACE_HEIGHT = 480; // initial sprite positions // These coordinates are for the top left corner of the sprite, using the // coordinate system of the surface. The origin is at the top left corner // of the surface. // Sprites that have a lower z are drawn in front of the other sprites. // WARNING: z does not show prespective. If Sprite A has a lower z than Sprite B, // Sprite A will be drawn in front of Sprite B. But if you keep increasing Sprite // B's z coordinate, Sprite B will NOT get smaller, like it is getting further from you. static final int SKY_X = 0; static final int SKY_Y = 0; static final int SKY_Z = 500; // Sprites can be placed at negative x and y (so the top left corner of the // sprite is placed to the left and above the top left corner of the surface) // z cannot be negative. 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"; static final String ELVIS_FILE = "../images/Ewave1.gif"; // StaticDataSprites for the applet. Every visible object in your // display is a sprite. StaticDataSprite Sky; StaticDataSprite Middle; StaticDataSprite Floor; StaticDataSprite Elvis; // awt.Image for Elvis Sprite Image ElvisImage; // SingleImage for Elvis Sprite SingleImage ElvisSource = null; // 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("Set Transparency")); ButtonPanel.add(new Button("Clear Transparency")); add("South", ButtonPanel); // 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 "UsingStaticDataSprites v.1.0"; } /** * Event Handler for button clicks. Set/Clear transparency color. * @param evt Event description * @param arg String on the face of the button */ public boolean action(Event evt, Object arg) { // set SingleImage transparency depending on button click if (arg.equals("Set Transparency")) { setTransparency(ElvisImage, ElvisSource); } else if (arg.equals("Clear Transparency")) { ElvisSource.clearTransparency(); } else // action not handled so return false return false; // repaint AnimCanvas to show color change canvas.repaint(); // return true to show that action was handled return true; } /** * Initialize 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 InitSprites() { // 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 sprite // create awt.Image ElvisImage = getImage(getDocumentBase(), ELVIS_FILE); try { // instantiate SingleImage ElvisSource = new SingleImage(ElvisImage); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // Set transparency for SingleImage setTransparency(ElvisImage, ElvisSource); // instantiate the sprite Elvis = new StaticDataSprite(); Elvis.setData(ElvisSource); // 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 UsingStaticDataSprites