Professional Documents
Culture Documents
Chapter 12 - The Applet Package
Chapter 12 - The Applet Package
CONTENTS
Introduction
Class Applet
Interface AppletContext
Interface AppletStub
Interface AudioClip
Introduction
The java.applet Package contains classes and interfaces for creating applets. This package also provides several interfaces
that connect an applet to its document and to resource for playing audio. This is in fact a fundamental package for the creation
of applets in Java. The various components in this package is listed below :
Classes
Class Applet
Interfaces
Interface AppletContext
Interface AppletStub
Interface AudioClip
Class Applet
An applet is a small program that is not intended to be run on its own, but to be embedded inside another application. The
Applet class must be the superclass of any applet that is to be embedded in a Web page or viewed by the Java Applet Viewer.
The Applet class provides a standard interface between applets and their environment. The structure of this class is stated as
below :
/
public class java.applet.Applet
extends java.awt.Panel
{
// Constructors
public Applet();
// The default constructor for an applet.
// Methods
public void destroy();
public AppletContext getAppletContext();
public String getAppletInfo();
public AudioClip getAudioClip(URL url);
public AudioClip getAudioClip(URL url, String name);
public URL getCodeBase();
public URL getDocumentBase();
public Image getImage(URL url);
public Image getImage(URL url, String name);
public String getParameter(String name);
public String[][] getParameterInfo();
public void init();
public boolean isActive();
public void play(URL url);
public void play(URL url, String name);
public void resize(Dimension d);
public void resize(int width, int height);
public final void setStub(AppletStub stub);
public void showStatus(String msg);
public void start();
public void stop();
}
Table 1.1
Method Description
destroy() This method is called by the browser or applet viewer to
inform thisapplet that it is being reclaimed and that it should
destroy any resources that it has allocated. The stop
method will always be called before destroy. A subclass of
Applet should override this method if it has any operation
that it wants to perform before it is destroyed. For example,
an applet with threads would use the init
(../../../../../../queued/7/d/6/7d6bab95-3dad-4ee5-afcc-
9f8332a24635/javam1.htm/) method to create the threads
and the destroy method to kill them.
/
getAudioClip(URL url) Returns the AudioClip object
(../../../../../../queued/7/d/6/7d6bab95-3dad-4ee5-afcc-
9f8332a24635/javam4.htm/) specified by the URL argument.
This method always returns immediately, whether or not the
audio clip exists. When this applet attempts to play the
audio clip, the data will be loaded.
getAudioClip(URL url, String Returns the AudioClip object specified by the URL and
name) name arguments.
getCodeBase()
Returns : The URL of this applet.
getDocumentBase()
Returns : The URL (../../../../../../queued/7/d/6/7d6bab95-
3dad-4ee5-afcc-9f8332a24635/javaf8.htm/) of the document
that contains this applet.
getImage(URL url) Returns an Image object that can then be painted on the screen.
The url that is passed as an argument must specify an absolute
URL.162162
/
Table 1.1 (Contd.)
play(URL url, String name) Plays the audio clip given the URL and a specifier that's
relative to it. Nothing happens if the audio clip cannot be
found.
Interface AppletContext
This interface corresponds to an applet's environment: the document containing the applet and the other applets in the same
document. The methods in this interface can be used by an applet to obtain information about its environment.
Table 1.2
/
Method Description
getApplet(String name) Finds and returns the applet in the document represented
by this applet context with the given name. The name can
be set in the HTML tag by setting the name attribute.
getApplets() Finds all the applets in the document represented by this
applet context.
Returns : an enumeration of all applets in the document
represented by this applet context.
getAudioClip(URL url) Creates an audio clip.
Returns : The audio clip at the specified URL.
getImage(URL url) Returns an Image object
(../../../../../../queued/7/d/6/7d6bab95-3dad-4ee5-afcc-
9f8332a24635/javai5.htm/) that can then be painted on the
screen. The url argument that is passed as an argument
must specify an absolute URL. This method always returns
immediately, whether or not the image exists. When the
applet attempts to draw the image on the screen, the data
will be loaded. The graphics primitives that draw the image
will incrementally paint on the screen.
showDocument(URL url) Replaces the Web page currently being viewed with the
given URL. This method may be ignored by applet contexts
that are not browsers.
showDocument(URL url, String Requests that the browser or applet viewer show the Web
target) page indicated by the url argument. The target argument
indicates where to display the frame. The target argument is
interpreted as follows :
Argument Function
self: Show the current frame
parent: Show the parent frame
top-most: Show the top most frame
An applet viewer or browser is free to ignore.
Interface AppletStub
When an applet is first created, an applet stub is attached to it using the applet's setStub method. This stub serves as the
interface between the applet and the browser environment or applet viewer environment in which the applet is running.
Table 1.3
Method Description
appletResize(int width, int height) Called when the applet wants to be resized.
/
getAppletContext()
Returnsc : the applet's context.
getCodeBase()
Returns : The URL of the applet.
getDocumentBase()
Returns : the URL of the document containing the applet.
getParameter(String name) Returns the value of the named parameter in the HTML tag.
165
isActive() Determines if the applet is active. An applet is active just
before its start method (../../../../../../queued/7/d/6/7d6bab95-
3dad-4ee5-afcc-9f8332a24635/javam1.htm/) is called. It
becomes inactive immediately after its stop
(../../../../../../queued/7/d/6/7d6bab95-3dad-4ee5-afcc-
9f8332a24635/javam1.htm/) method is called. Returns : true if
the applet is active; false otherwise.
Interface AudioClip
The AudioClip interface is a simple abstraction for playing a sound clip. Multiple AudioClip items can be playing at the same
time, and the resulting sound is mixed together to produce a composite.
Table 1.4
Method Description
loop() Starts playing this audio clip in a loop.
play() Starts playing this audio clip. Each time this method is
called, the clip is restarted from the beginning.
stop() Stops playing this audio clip.
(index.htm)
(ch9.htm) (ch11.htm)