void showStatus(String str)
Displays str in the status window of the browser or
applet viewer. If the browser does not suppor t a status
window, then no action takes place.
void star t( )
Called by the browser when an applet should star t
(or resume) execution. It is automatically called after
init( ) when an applet first begins.
void stop( )
Called by the browser to suspend execution of the
applet. Once stopped, an applet is restar ted when
the browser calls star t( ).
The Methods Defined by Applet (continued)
An applet is a window-based program. As such, its architecture is different from the console-based
programs shown in the first part of this topic. If you are familiar with Windows programming,
you will be right at home writing applets. If not, then there are a few key concepts you must
First, applets are event driven. Although we won't examine event handling until the
following chapter, it is important to understand in a general way how the event-driven
architecture impacts the design of an applet. An applet resembles a set of interrupt service
routines. Here is how the process works. An applet waits until an event occurs. The run-time
system notifies the applet about an event by calling an event handler that has been provided
by the applet. Once this happens, the applet must take appropriate action and then quickly
return. This is a crucial point. For the most part, your applet should not enter a "mode" of
operation in which it maintains control for an extended period. Instead, it must perform
specific actions in response to events and then return control to the run-time system. In those
situations in which your applet needs to perform a repetitive task on its own (for example,
displaying a scrolling message across its window), you must start an additional thread of
execution. (You will see an example later in this chapter.)
Second, the user initiates interaction with an applet--not the other way around. As you
know, in a nonwindowed program, when the program needs input, it will prompt the user
and then call some input method, such as readLine( ). This is not the way it works in an
applet. Instead, the user interacts with the applet as he or she wants, when he or she wants.
These interactions are sent to the applet as events to which the applet must respond. For
example, when the user clicks the mouse inside the applet's window, a mouse-clicked event
is generated. If the user presses a key while the applet's window has input focus, a keypress
event is generated. As you will see in later chapters, applets can contain various controls,
such as push buttons and check boxes. When the user interacts with one of these controls,
an event is generated.
While the architecture of an applet is not as easy to understand as that of a console-based
program, Java makes it as simple as possible. If you have written programs for Windows,
you know how intimidating that environment can be. Fortunately, Java provides a much
cleaner approach that is more quickly mastered.
Search WWH :