21 The Applet Class
21 The Applet Class
T
his chapter examines theAppletclass, which provides the foundation for applets.
TheAppletclass is contained in thejava.appletpackage.Appletcontains several
methods that give you detailed control over the execution of your applet. In addition,
java.appletalso defines three interfaces:AppletContext,AudioClip, andAppletStub.
Two Types of Applets
It is important to state at the outset that there are two varieties of applets. The first are those
based directly on theAppletclass described in this chapter. These applets use the Abstract
Window Toolkit (AWT) to provide the graphic user interface (or use no GUI at all). This
style of applet has been available since Java was first created.
The second type of applets are those based on the Swing classJApplet. Swing applets use
the Swing classes to provide the GUI. Swing offers a richer and often easier-to-use user interface
than does the AWT. Thus, Swing-based applets are now the most popular. However, traditional
AWT-based applets are still used, especially when only a very simple user interface is required.
Thus, both AWT- and Swing-based applets are valid.
BecauseJAppletinheritsApplet, all the features ofAppletare also available inJApplet,
and most of the information in this chapter applies to both types of applets. Therefore, even if
you are interested in only Swing applets, the information in this chapter is still relevant and
necessary. Understand, however, that when creating Swing-based applets, some additional
constraints apply and these are described later in this book, when Swing is covered.
NOTEOTE For information on building applets when using Swing, see Chapter 29.
Applet Basics
Chapter 13 introduced the general form of an applet and the steps necessary to compile and
run one. Let’s begin by reviewing this information.
All applets are subclasses (either directly or indirectly) ofApplet. Applets are not
stand-alone programs. Instead, they run within either a web browser or an applet viewer.
The illustrations shown in this chapter were created with the standard applet viewer, called
appletviewer, provided by the JDK. But you can use any applet viewer or browser you like.
Execution of an applet does not begin atmain( ). Actually, few applets even havemain( )
methods. Instead, execution of an applet is started and controlled with an entirely different
617