/**************************************************************************** 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 anim.*; /** * Using Surface and AnimCanvas. Demonstrates usage of Surface class. Displays a blank * surface and changes the surface color based on button presses. */ public class UsingSurface extends Applet { // surface size static final int SURFACE_WIDTH = 300; static final int SURFACE_HEIGHT = 300; // 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("Black")); ButtonPanel.add(new Button("Red")); ButtonPanel.add(new Button("Green")); ButtonPanel.add(new Button("Blue")); add("South", ButtonPanel); 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 "UsingSurface v.1.0"; } /** * Event Handler for button clicks. Change surface color. * @param evt Event description * @param arg String on the face of the button */ public boolean action(Event evt, Object arg) { // change surface color depending on button click // use AnimCanvas.getSurface to get access to the surface // and then call Surface.setColor to change the color if (arg.equals("Black")) { canvas.getSurface().setColor(Color.black); } else if (arg.equals("Red")) { canvas.getSurface().setColor(Color.red); } else if (arg.equals("Green")) { canvas.getSurface().setColor(Color.green); } else if (arg.equals("Blue")) { canvas.getSurface().setColor(Color.blue); } 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; } } // applet UsingSurface