/**************************************************************************** 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.*; /** * UsingEffects. Demonstrates usage of various Effects classes. * Displays a starfield background and Intel sprite. * Various effects are activated on Intel sprite with button clicks. */ public class UsingEffects 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; // coordinates for center of Intel sprite. used for effects parameters // y coordinate for x axis static final int INTEL_X_AXIS = 64; // x coordinate for y axis static final int INTEL_Y_AXIS = 125; // minimum and maximum scale parameters static final float MAX_SCALE = (float) 2.0; static final float MIN_SCALE = (float) 0.2; // file locations for sprite images static final String STARS_FILE = "../images/Stars.gif"; static final String INTEL_FILE = "../images/Intel.gif"; // StaticDataSprites for the applet. Every visible object in your // display is a sprite. StaticDataSprite Stars; StaticDataSprite Intel; // animcanvas AnimCanvas canvas; // effects and parameters RotateXEffect RotateXFX; RotateYEffect RotateYFX; RotateZEffect RotateZFX; ScaleEffect ScaleFX; HorizontalFlipEffect HorizFlipFX; VerticalFlipEffect VertFlipFX; RotateXParam RotateXFXParam; RotateYParam RotateYFXParam; RotateZParam RotateZFXParam; ScaleParam ScaleFXParam; // flag to determine is ScaleParams should increase or decrease boolean expand = false; // buttons to activate/deactivate effect Button RotateXButton = new Button("Rotate X"); Button RotateYButton = new Button("Rotate Y"); Button RotateZButton = new Button("Rotate Z"); Button ScaleButton = new Button("Scale"); Button HorizFlipButton = new Button("H-Flip"); Button VertFlipButton = new Button("V-Flip"); // thread to modify effect parameters Thread FXThread; /** * 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(RotateXButton); ButtonPanel.add(RotateYButton); ButtonPanel.add(RotateZButton); ButtonPanel.add(ScaleButton); ButtonPanel.add(HorizFlipButton); ButtonPanel.add(VertFlipButton); add("South", ButtonPanel); // initialize sprites InitSprites(); // initialize effects InitEffects(); System.out.println("Initializing applet: DONE"); } /** * Applet start method. Start FXThread */ public void start() { System.out.println("Starting the applet"); FXThread = new Thread(this); FXThread.start(); } /** * Applet stop method. FXThread */ public void stop() { System.out.println("Stopping the applet"); FXThread.stop(); } /** * Applet destroy method. */ public void destroy() { System.out.println("Destroying the applet!"); } /** * Return applet info. * @return Applet info */ public String getAppletInfo() { return "UsingEffects v.1.0"; } /** * Event Handler for button clicks. Activate/Deactivate effects. * @param evt Event description * @param arg String on the face of the button */ public boolean action(Event evt, Object arg) { // Activate/Deactivate effect and modify button label if (arg.equals("Rotate X")) { RotateXFX.activate(true); RotateXButton.setLabel("Remove X"); } else if (arg.equals("Rotate Y")) { RotateYFX.activate(true); RotateYButton.setLabel("Remove Y"); } else if (arg.equals("Rotate Z")) { RotateZFX.activate(true); RotateZButton.setLabel("Remove Z"); } else if (arg.equals("Scale")) { ScaleFX.activate(true); ScaleButton.setLabel("Remove Scale"); } else if (arg.equals("H-Flip")) { HorizFlipFX.activate(true); HorizFlipButton.setLabel("Remove H-Flip"); } else if (arg.equals("V-Flip")) { VertFlipFX.activate(true); VertFlipButton.setLabel("Remove V-Flip"); } else if (arg.equals("Remove X")) { RotateXFX.activate(false); RotateXButton.setLabel("Rotate X"); } else if (arg.equals("Remove Y")) { RotateYFX.activate(false); RotateYButton.setLabel("Rotate Y"); } else if (arg.equals("Remove Z")) { RotateZFX.activate(false); RotateZButton.setLabel("Rotate Z"); } else if (arg.equals("Remove Scale")) { ScaleFX.activate(false); ScaleButton.setLabel("Scale"); } else if (arg.equals("Remove H-Flip")) { HorizFlipFX.activate(false); HorizFlipButton.setLabel("H-Flip"); } else if (arg.equals("Remove V-Flip")) { VertFlipFX.activate(false); VertFlipButton.setLabel("V-Flip"); } 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; } /** * Run method for FXThread. This method is called when * runner.start() is called. This method keeps adjusting the * parameters for the effects */ public void run() { while(true) { // adjust rotate x parameters RotateXFXParam.Angle = (RotateXFXParam.Angle + 15.0f) % 360; // set new rotate x effect parameters RotateXFX.setParams(RotateXFXParam); // adjust rotate y parameters RotateYFXParam.Angle = (RotateYFXParam.Angle + 15.0f) % 360; // set new rotate y effect parameters RotateYFX.setParams(RotateYFXParam); // adjust rotate z parameters RotateZFXParam.Angle = (RotateZFXParam.Angle + 15.0f) % 360; // set new rotate z effect parameters RotateZFX.setParams(RotateZFXParam); // adjust scale effect // check if expand flag should be changed (scale too big or too small) if( (ScaleFXParam.xScale > MAX_SCALE) || (ScaleFXParam.yScale > MAX_SCALE) ) { expand = false; } else if( (ScaleFXParam.xScale < MIN_SCALE) || (ScaleFXParam.yScale < MIN_SCALE) ) { expand = true; } // change scale parameters accordingly if( expand ) { ScaleFXParam.xScale += 0.1f; ScaleFXParam.yScale += 0.1f; } else { ScaleFXParam.xScale -= 0.1f; ScaleFXParam.yScale -= 0.1f; } // set new scale effect parameters ScaleFX.setParams( ScaleFXParam ); // repaint AnimCanvas to show new image 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; // Initialize Stars sprite // create awt.Image image = getImage(getDocumentBase(), STARS_FILE); try { // instantiate SingleImage spriteSource = new SingleImage(image); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // instantiate the sprite Stars = new StaticDataSprite(); Stars.setData(spriteSource); // 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)); } /** * 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); } /** * Initialize effects: * Instantiate effects and effect parameters and apply to sprite * then deactivate effects */ private void InitEffects() { // Initialize Rotate X effect // rotate about center x axis of sprite RotateXFXParam = new RotateXParam(0.0f, INTEL_X_AXIS); RotateXFX= new RotateXEffect(RotateXFXParam); // apply new Effect Intel.applyEffect(RotateXFX); // deactivate effect RotateXFX.activate(false); // Initialize Rotate Y effect // rotate about center y axis of sprite RotateYFXParam = new RotateYParam(0.0f, INTEL_Y_AXIS); RotateYFX= new RotateYEffect(RotateYFXParam); // apply new Effect Intel.applyEffect(RotateYFX); // deactivate effect RotateYFX.activate(false); // initialize Rotate Z effect // rotate about center of sprite RotateZFXParam = new RotateZParam(0.0f, INTEL_Y_AXIS, INTEL_X_AXIS); RotateZFX = new RotateZEffect(RotateZFXParam); // apply new Effect Intel.applyEffect(RotateZFX); // deactivate effect RotateZFX.activate(false); // initialize Scale effect // scale from origin of sprite ScaleFXParam = new ScaleParam(1.0f, 1.0f, INTEL_Y_AXIS, INTEL_X_AXIS); ScaleFX= new ScaleEffect(ScaleFXParam); // apply new Effect Intel.applyEffect(ScaleFX); // deactivate effect ScaleFX.activate(false); // initialize Horizontal Flip Effect HorizFlipFX = new HorizontalFlipEffect(); // apply new Effect Intel.applyEffect(HorizFlipFX); // deactivate effect HorizFlipFX.activate(false); // initialize Vertical Flip Effect VertFlipFX = new VerticalFlipEffect(); // apply new Effect Intel.applyEffect(VertFlipFX); // deactivate effect VertFlipFX.activate(false); } } // applet UsingEffects