Java Reference
In-Depth Information
This applet begins with two import statements. The first imports the Abstract Window
Toolkit classes. AWT-based applets interact with the user through the AWT, not through the
console-based I/O classes. The AWT contains support for a limited window-based, graph-
ical user interface. As you might expect, it is quite large and sophisticated. A complete dis-
cussion of it would require a book of its own. Fortunately, since we will be creating only
very simple applets, we will make only limited use of the AWT. The next import statement
imports the applet package. This package contains the class Applet . Every AWT-based ap-
plet that you create must be a subclass of Applet .
The next line in the program declares the class SimpleApplet . This class must be de-
clared as public because it will be accessed by outside code.
Inside SimpleApplet , paint( ) is declared. This method is defined by the AWT Com-
ponent class (which is a superclass of Applet ) and is overridden by the applet. paint( ) is
called each time the applet must redisplay its output. This can occur for several reasons.
For example, the window in which the applet is running can be overwritten by another win-
dow and then uncovered. Or the applet window can be minimized and then restored. paint(
) is also called when the applet begins execution. Whatever the cause, whenever the applet
must redraw its output, paint( ) is called. The paint( ) method has one parameter of type
Graphics . This parameter will contain the graphics context, which describes the graphics
environment in which the applet is running. This context is used whenever output to the
applet is required.
Inside paint( ) , there is a call to drawString( ) , which is a member of the Graphics
class. This method outputs a string beginning at the specified X,Y location. It has the fol-
lowing general form:
void drawString(String message , int x , int y )
Search WWH ::




Custom Search