probably running. You should use stop( ) to suspend threads that don't need to run when the
applet is not visible. You can restart them when start( ) is called if the user returns to the page.
The destroy( ) method is called when the environment determines that your applet needs to
be removed completely from memory. At this point, you should free up any resources the
applet may be using. The stop( ) method is always called before destroy( ).
Overriding update( )
In some situations, your applet may need to override another method defined by the AWT,
called update( ). This method is called when your applet has requested that a portion of its
window be redrawn. The default version of update( ) simply calls paint( ). However, you
can override the update( ) method so that it performs more subtle repainting. In general,
overriding update( ) is a specialized technique that is not applicable to all applets, and the
examples in this topic do not override update( ).
Simple Applet Display Methods
As we've mentioned, applets are displayed in a window, and AWT-based applets use the
AWT to perform input and output. Although we will examine the methods,
procedures, and techniques necessary to fully handle the AWT windowed environment
in subsequent chapters, a few are described here, because we will use them to write
sample applets. (Remember, Swing-based applets are described later in this topic.)
As described in Chapter 13, to output a string to an applet, use drawString( ), which is a
member of the Graphics class. Typically, it is called from within either update( ) or paint( ).
It has the following general form:
void drawString(String message, int x, int y)
Here, message is the string to be output beginning at x,y. In a Java window, the upper-left
corner is location 0,0. The drawString( ) method will not recognize newline characters. If
you want to start a line of text on another line, you must do so manually, specifying the
precise X,Y location where you want the line to begin. (As you will see in later chapters,
there are techniques that make this process easy.)
To set the background color of an applet's window, use setBackground( ). To set the
foreground color (the color in which text is shown, for example), use setForeground( ).
These methods are defined by Component, and they have the following general forms:
void setBackground(Color newColor)
void setForeground(Color newColor)
Here, newColor specifies the new color. The class Color defines the constants shown here
that can be used to specify colors:
Search WWH :