Java Reference
In-Depth Information
Example 15−4: EventTester.java (continued)
else return String.valueOf(c);
}
// Return the name of a function key. Just compare the key to the
// constants defined in the Event class.
private String function_key_name(int key) {
switch(key) {
case Event.HOME: return "Home"; case Event.END: return "End";
case Event.PGUP: return "Page Up"; case Event.PGDN: return"Page Down";
case Event.UP: return "Up";
case Event.DOWN: return "Down";
case Event.LEFT: return "Left";
case Event.RIGHT: return "Right";
case Event.F1: return "F1";
case Event.F2: return "F2";
case Event.F3: return "F3";
case Event.F4: return "F4";
case Event.F5: return "F5";
case Event.F6: return "F6";
case Event.F7: return "F7";
case Event.F8: return "F8";
case Event.F9: return "F9";
case Event.F10: return "F10";
case Event.F11: return "F11";
case Event.F12: return "F12";
}
return "Unknown Function Key";
}
/** A list of lines to display in the window */
protected Vector lines = new Vector();
/** Add a new line to the list of lines, and redisplay */
protected void showLine(String s) {
if (lines.size() == 20) lines.removeElementAt(0);
lines.addElement(s);
repaint();
}
/** This method repaints the text in the window */
public void paint(Graphics g) {
for(int i = 0; i < lines.size(); i++)
g.drawString((String)lines.elementAt(i), 20, i*16 + 50);
}
}
Reading Applet Parameters
Example 15-5 shows an extension to our Scribble applet. The ColorScribble
class is a subclass of Scribble that adds the ability to scribble in a configurable
foreground color over a configurable background color.
ColorScribble has an init() method that reads the value of two applet parame-
ters that can be optionally specified with the <PARAM> tag in the applet's HTML file.
The String values returned by getParameter() are converted to colors and speci-
fied as the default foreground and background colors for the applet. Note that the
init() method invokes its superclass' init() method, so that the superclass has
the opportunity to initialize itself.
This example also demonstrates the getAppletInfo() and getParameterInfo()
methods. These methods provide textual information about the applet (its author,
its version, its copyright, etc.) and the parameters that it can accept (the parameter
names, their types, and their meanings). An applet should generally define these
methods, although the current generation of web browsers do not actually ever
Search WWH ::




Custom Search