img
mechanism, which will be explained shortly. Output to your applet's window is not performed by
System.out.println( ). Rather, in non-Swing applets, output 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 a console application. (Remember, Swing-based applets use the Swing
classes to handle user interactions, and they are described later in this topic.)
To use an applet, it is specified in an HTML file. One way to do this is by using the APPLET
tag. (The OBJECT tag can also be used, but Sun currently recommends the APPLET tag and this is
the tag used by examples in this topic.) The applet will be executed by a Java-enabled web
browser when it encounters the APPLET tag within the HTML file. To view and test an applet
more conveniently, simply include 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 HTML
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:
/*
<applet code="MyApplet" width=200 height=60>
</applet>
*/
This comment contains an APPLET tag that will run an applet called MyApplet in a
window that is 200 pixels wide and 60 pixels high. Because the inclusion of an APPLET
command makes testing applets easier, all of the applets shown in this topic will contain the
appropriate APPLET tag embedded in a comment.
The Applet Class
The Applet class defines the methods shown in Table 21-1. Applet provides all necessary
support for applet execution, such as starting and stopping. It also provides methods that
load and display images, and methods that load and play audio clips. 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, Applet provides
all of the necessary support for window-based activities. (The AWT is described in detail in
following chapters.)
Method
Description
void destroy( )
Called by the browser just before an applet is
terminated. Your applet will override this method if it
needs to per form any cleanup prior to its destruction.
AccessibleContext
Returns the accessibility context for the invoking object.
getAccessibleContext( )
AppletContext getAppletContext( )
Returns the context associated with the applet.
String getAppletInfo( )
Returns a string that describes the applet.
AudioClip getAudioClip(URL url)
Returns an AudioClip object that encapsulates the
audio clip found at the location specified by url.
TABLE 21-1
The Methods Defined by Applet
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home