/**************************************************************************** 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.*; /** * PuttingItAllTogether2. Shows animated starfield background, * rotating (on x axis) Intel logo, and scrolling "Intel's anim Package" text */ public class PuttingItAllTogether2 extends Applet implements Runnable { // surface size static final int SURFACE_WIDTH = 500; static final int SURFACE_HEIGHT = 400; // initial sprite positions static final int STARS_X = 0; static final int STARS_Y = 0; static final int STARS_Z = 500; static final int INTEL_X = 125; static final int INTEL_Y = 100; static final int INTEL_Z = 400; static final int TEXTSPRITE_X = 500; static final int TEXTSPRITE_Y = 340; static final int TEXTSPRITE_Z = 400; // coordinates for center of Intel sprite. used for effects parameters // y coordinate for x axis static final int INTEL_X_AXIS = 64; // start and end positions for scrolling TextSprite static final int TEXTSPRITE_START_X = 500; static final int TEXTSPRITE_END_X = -225; // amount to move TextSprite static final int TEXTSPRITE_MOVEMENT = 4; // text to display static final String TEXT_TO_DISPLAY = "Intel's anim Package"; // location of text on the awt.Image static final int TEXT_X = 5; static final int TEXT_Y = 25; // Font type, style, size, and color static final String FONT_TYPE = "TimesRoman"; static final int FONT_STYLE = Font.PLAIN; static final int FONT_SIZE = 25; static final Color FONT_COLOR = Color.green; // TextSprite dimensions, in pixels static final int TEXTSPRITE_WIDTH = 220; static final int TEXTSPRITE_HEIGHT = 30; // file locations for sprite images static final String STARS_FILE[] = { "../images/stars01.gif", "../images/stars02.gif", "../images/stars03.gif", "../images/stars04.gif", "../images/stars05.gif", "../images/stars06.gif", "../images/stars07.gif", "../images/stars08.gif", "../images/stars09.gif", "../images/stars10.gif"}; static final String INTEL_FILE = "../images/Intel.gif"; // number of frames in Stars animation static final int NUMFRAMES_STARS = 10; // dimensions of Stars images static final int STARS_WIDTH = 500; static final int STARS_HEIGHT = 400; // StaticDataSprites for the applet. Every visible object in your // display is a sprite. StaticDataSprite Stars; StaticDataSprite TextSprite; StaticDataSprite Intel; // Position for TextSprite Point3D TextSpritePos; // Rotate X Effect and Parameter RotateXEffect RotateXFX; RotateXParam RotateXFXParam; // animcanvas AnimCanvas canvas; // thread that does animation, movement, and effects Thread runner; /** * 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); // initialize sprites InitSprites(); // initialize effect InitEffect(); System.out.println("Initializing applet: DONE"); } /** * Applet start method. Start runner thread. */ public void start() { System.out.println("Starting the applet"); // start runner thread runner = new Thread(this); runner.start(); } /** * Applet stop method. Stop runner thread. */ public void stop() { System.out.println("Stopping the applet"); // stop runner thread runner.stop(); } /** * Applet destroy method. */ public void destroy() { System.out.println("Destroying the applet!"); } /** * Return applet info. * @return Applet info */ public String getAppletInfo() { return "PuttingItAllTogether2 v.1.0"; } /** * Run method for runner thread. This method is called when * runner.start() is called. This method keeps adjusting the * current image of the Stars Sprite, moves the TextSprite, and * changes the parameters of the RotateXEffect */ public void run() { while( true ) { // adjust current image of Stars by 1 Stars.adjustCurrentImageNumber(1); // Move TextSprite // if TextSprite is off the screen, send it to the other side TextSpritePos.x = TextSpritePos.x - TEXTSPRITE_MOVEMENT; if (TextSpritePos.x < TEXTSPRITE_END_X) { TextSpritePos.x = TEXTSPRITE_START_X; } TextSprite.setPosition(TextSpritePos); // adjust rotate x parameters RotateXFXParam.Angle = (RotateXFXParam.Angle + 15.0f) % 360; // set new rotate x effect parameter RotateXFX.setParams(RotateXFXParam); // repaint AnimCanvas to show new images canvas.repaint(); } } /** * 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; // ImageSequence for Stars Sprite ImageSequence seq = null; // Initialize Stars sprite // create ImageSequence seq = new ImageSequence(NUMFRAMES_STARS, STARS_WIDTH, STARS_HEIGHT); // create SingleImages and add to ImageSequence for(int seqnum = 0; seqnum < NUMFRAMES_STARS; seqnum++) { // create awt.Image image = getImage(getDocumentBase(), STARS_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 Stars = new StaticDataSprite(); Stars.setData(seq); // set sprite to surface Stars.setDestination(canvas.getSurface()); // position sprite Stars.setPosition(new Point3D(STARS_X, STARS_Y, STARS_Z)); // Initialize Intel sprite // create awt.Image image = getImage(getDocumentBase(), INTEL_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 Intel = new StaticDataSprite(); Intel.setData(spriteSource); // set sprite to surface Intel.setDestination(canvas.getSurface()); // position sprite Intel.setPosition(new Point3D(INTEL_X, INTEL_Y, INTEL_Z)); // Initialize TextSprite // creat awt.Image to draw text onto image = this.createImage(TEXTSPRITE_WIDTH, TEXTSPRITE_HEIGHT); // graphics context needed to draw Graphics gc = image.getGraphics(); // Font for text Font f = new Font(FONT_TYPE, FONT_STYLE, FONT_SIZE); // Draw text onto image gc.setFont(f); gc.setColor(FONT_COLOR); gc.drawString(TEXT_TO_DISPLAY, TEXT_X, TEXT_Y); // instantiate SingleImage try { spriteSource = new SingleImage(image); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // set spriteSource transparency setTransparency(image, spriteSource); // instantiate the sprite TextSprite = new StaticDataSprite(); TextSprite.setData(spriteSource); // set sprite to surface TextSprite.setDestination(canvas.getSurface()); // position sprite TextSpritePos = new Point3D(TEXTSPRITE_X, TEXTSPRITE_Y, TEXTSPRITE_Z); TextSprite.setPosition(TextSpritePos); } /** * Initialize effect: * Instantiate effect and effect parameter and apply effect */ private void InitEffect() { // Initialize Rotate X effect // rotate about center x axis of sprite RotateXFXParam = new RotateXParam(0.0f, INTEL_X_AXIS); RotateXFX= new RotateXEffect(RotateXFXParam); // apply Effect Intel.applyEffect(RotateXFX); } /** * 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 PuttingItAllTogether2