/**************************************************************************** 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.*; /** * UsingTextAndSprites. Integrates text and sprites. * Displays a starfield background and a sprite containing text. * User can enter text and sprite will display it. */ public class UsingTextAndSprites extends Applet { // 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 TEXTSPRITE_X = 150; static final int TEXTSPRITE_Y = 170; static final int TEXTSPRITE_Z = 400; // file locations for sprite images static final String STARS_FILE = "../images/Stars.gif"; // StaticDataSprites for the applet. Every visible object in your // display is a sprite. StaticDataSprite Stars; DynamicDataSprite TextSprite; // animcanvas AnimCanvas canvas; // text input component TextField TextToDisplay; // Length of text input static final int TEXTFIELD_LENGTH = 23; // Maximum number of characters allowed (this just makes formatting easier) static final int MAX_CHARACTERS = 20; // TextSprite dimensions, in pixels static final int TEXTSPRITE_WIDTH = 235; static final int TEXTSPRITE_HEIGHT = 30; // Initial text static final String INITIAL_TEXT = "Text and Sprites"; // location of text on the awt.Image static final int TEXT_X = 10; 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; /** * 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 TextField and Button Panel ButtonPanel = new Panel(); ButtonPanel.setLayout(new FlowLayout()); // add components TextToDisplay = new TextField("", TEXTFIELD_LENGTH); ButtonPanel.add(TextToDisplay); ButtonPanel.add(new Button("Display Text")); add("South", ButtonPanel); // initialize sprites InitSprites(); // initialize TextSprite InitTextSprite(); 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 "UsingTextAndSprites v.1.0"; } /** * Event Handler for button clicks. Display contents of TextField in TextSprite. * @param evt Event description * @param arg String on the face of the button */ public boolean action(Event evt, Object arg) { // Display contents of TextField in TextSprite if (arg.equals("Display Text")) { // get text to display String text = TextToDisplay.getText().trim(); // take only first MAX_CHARACTERS characters if (text.length() > MAX_CHARACTERS) { text = text.substring(0, MAX_CHARACTERS); } // create new SingleImage and assign to TextSprite SetTextImage(text); } 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 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)); } /** * 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 TextSprite: * Create TextSprite * Set SingleImage for TextSprite */ private void InitTextSprite() { // instantiate the sprite TextSprite= new DynamicDataSprite(); // create SingleImage and assign to TextSprite SetTextImage(INITIAL_TEXT); // set sprite to surface TextSprite.setDestination(canvas.getSurface()); // position sprite TextSprite.setPosition(new Point3D(TEXTSPRITE_X, TEXTSPRITE_Y, TEXTSPRITE_Z)); } /** * SetTextImage. Create new Text SingleImage and assign to TextSprite * @param text Text for TextSprite */ private void SetTextImage(String text) { // new SingleImage for TextSprite SingleImage TextImage; // awt.Image to draw text onto Image textimage = this.createImage(TEXTSPRITE_WIDTH, TEXTSPRITE_HEIGHT); // graphics context needed to draw Graphics gc = textimage.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, TEXT_X, TEXT_Y); // create SingleImage try { TextImage = new SingleImage(textimage); } catch(InterruptedException e) { throw new AnimInternalError("Image load timed out"); } // set TextImage transparency setTransparency(textimage, TextImage); // have TextSprite use new TextImage TextSprite.setData(TextImage); } } // applet UsingTextAndSprites