The Applet’ Class
This chapter examines the Applet class, which .provides the necessary support for applets. In Chapter 12, you were Introduced to the general form’ ofan applet and
the steps necessary to compile and run one. In this chapter, we will look at applets in detail The Applet class is contained in the package, Applet contains several methods that give you detailed control over the execution of your applet. In addition, java.applet also defines three interfaces: and Apple . Let’s begin by reviewing the basic elements of an and the steps necessary to create and test one
All applets are sub classes of Applet. Thus, all applets must import java.applet.Applets . must also import java.awt, Recall that AWT stands (or the Abstract Window Toolkit. Since all applets run-in a window, it is necessary to include support for that window. Applets are not exec;uted by the console-based Java run-time interpreter. Rather, they are executed by either a Web browser or an applet viewer. The figures shown in this chapter were, created with the standard applet viewer, called appletviewer, provided by the JOK. But you can use any applet viewer or browser you like.Execution of an applet does not begin at main] ). Actually, few applets even have main( ) methods. Instead, execution of an applet is started and controlled with an entirely different mechanism, which will be explained shortly. Output to your applet’s window is not performed by Syslem.out.println( ). Rather, it is handled with various AWT methods, such as drawString(), which outputs a string to a specified X-y . location. Input is also handled differently than in an application. Onee an applet has been compiled, it is’ included in an HTML file using. the APPLF.T tag. The applet will be executed by a Iava-cnabled web browser when it encounters the APPLET tag within.the HlML file. To view and test an applet more, conveniently, simply incljrde a comment at the head of your Java source code file that contains the APPLET tag~This way, your code is documented with the necessary I-ITML statements needed-by your applet, and you can test the compiled applet by .starting the applet viewer with your Java source code ‘file specified as the target. Here is an example of such a comment.
command makes testing applets easier, all of the applets shown in this book will contain the appropriate APPLET tag embedded.
The Applet Class
The Applet class defines the methods shown in Table 19-1. Applet provides all necessary support for applet execution, such as starting and slopping. It also provides methods that load and display images, and methods that load and play audio dips. . Applet extends the AWT class Panel. In turn, Panel extends Container, which extends· Component. These classes provide support for Java’s window-based, graphical interface, Thus, Applct provides all of the necessary support for window-based activities. (The AWT.is described in detail in following chapters.)·