// third in the series, adds a button component to control text color // these first lines tell javac which class libraries we plan to use import java.applet.*; import java.awt.*; // define a new subclass of applet, with 3 variables plus an array with index public class HelloWorld3 extends Applet { int messageX = 125, messageY = 80; String messageparam; Button ourButton; Color [] someColors = { Color.blue, Color.green, Color.yellow, Color.orange, Color.red, Color.magenta }; int colorIndex; // define the init method, gets input from .html, labels and adds button to screen public void init() { messageparam = this.getParameter ("message"); ourButton = new Button ("Moderately Magical Button"); add (ourButton); } // define a paint method, which is driven by three variables defined elsewhere public void paint (Graphics messagetext) { messagetext.setColor (currentColor()); messagetext.drawString (messageparam, messageX, messageY); } // define method to handle the button push event by getting a new color public boolean action(Event e, Object o) { if (e.target == ourButton) { play(getCodeBase(), "audio/yahoo1.au"); changeColor(); return true; } return false; } // define an event handling method, which reposiitons messagetext at new x,y coordinates public boolean mouseMove (Event evt, int x, int y) { messageX = x; messageY = y; repaint(); return true; } // define method to make a new color the current one synchronized private Color currentColor() { return someColors[colorIndex]; } // define method to make a new color the current one synchronized private void changeColor() { if (++colorIndex == someColors.length) colorIndex = 0; ourButton.setForeground(currentColor()); repaint(); } }