Java Reference
In-Depth Information
Listing 10-3.
Using Java AWT Components and Containers
package com.apress.rischpater.HelloApplet;
import java.awt.*;
import java.applet.*;
public class HelloApplet extends Applet {
public void init() {
Label label;
label = new Label("Hello world!");
add(label);
}
}
The difference between these two examples lies in how drawing is performed. The
code in Listing 10-2 uses the window toolkit's
Graphics
object directly, while the code in
Listing 10-3 delegates drawing to components provided by the window toolkit. Because
applets are only supported by the PP, you're assured that at least the Java AWT is available
on platforms that support applets.
Accessing an Applet's Context
An applet has access to resources on the server that provided the applet, including
images and audio clips. These can be fetched directly either by the applet or the applet's
context. You can obtain the applet's context (an instance of the
AppletContext
class) by
invoking the applet's
getAppletContext
method. The
AppletContext
represents the envi-
ronment in which one or more applets are executing, such as a web page or Java-based
application manager. With an instance of an
AppletContext
, you can perform the follow-
ing operations:
• Use
getApplet
to obtain a reference to a named applet running within the
AppletContext
.
• Use
getApplets
to enumerate across the list of applets managed by a specific
AppletContext
.
• Use
getAudioClip
to obtain an audio clip from the applet's server, or use
getImage
to obtain an image from the applet's server.
• Use
showDocument
to dispatch a request to view a web page.
• Use
showStatus
to update the context's displayed status.