Java Reference
In-Depth Information
(for example, displaying a scrolling message across its window), you must start an addi-
tional thread of execution.
Second, it is the user who initiates interaction with an applet—not the other way around.
In a console-based program, when the program needs input, it will prompt the user and then
call some input method. 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 a
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. 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
(or another GUI-based operating system), you know how intimidating that environment
can be. Fortunately, Java provides a much cleaner approach that is more quickly mastered.
A Complete Applet Skeleton
Although SimpleApplet shown earlier is a real applet, it does not contain all of the ele-
ments required by most applets. Actually, all but the most trivial applets override a set of
methods that provide the basic mechanism by which the browser or applet viewer interfaces
to the applet and controls its execution. These life-cycle methods are init( ) , start( ) , stop(
) , and destroy( ) , and they are defined by Applet . A fifth method, paint( ) , is commonly
overridden by AWT-based applets even though it is not a life-cycle method. It is inherited
from the AWT Component class. Since default implementations for all of these methods
are provided, applets do not need to override those methods they do not use. These four
life-cycle methods plus paint( ) can be assembled into the skeleton shown here:
Search WWH ::




Custom Search