Book Home Java Enterprise in a Nutshell Search this book

Chapter 8. The java.applet Package

The java.applet package is a small but important package that defines the Applet class--the superclass of all applets. It also defines the AppletContext and AppletStub interfaces, which are implemented by web browsers and other applet viewers. Finally, for lack of a better place, this package contains the AudioClip interface, which represents a sound to be played. Figure 8-1 shows the class hierarchy of this package. See Chapter 7, "Applets", for more details about applets.


Figure 8-1. The java.applet package

AppletJava 1.0
java.appletserializable AWT component PJ1.1

This class implements an applet. To create your own applet, you should create a subclass of this class and override some or all of the following methods. Note that you never need to call these methods--they are called when appropriate by a web browser or other applet viewer.

init() should perform any initialization for the applet; it is called when the applet first starts. destroy() should free up any resources that the applet is holding; it is called when the applet is about to be permanently stopped. start() is called to make applet start doing whatever it is that it does. Often, it starts a thread to perform an animation or similar task. stop() should temporarily stop the applet from executing. It is called when the applet temporarily becomes hidden or nonvisible.

getAppletInfo() should return text suitable for display in an About dialog posted by the web browser or applet viewer. getParameterInfo() should return an arbitrary-length array of three-element arrays of strings, where each element describes one of the parameters that this applet understands. The three elements of each parameter description are strings that specify the parameter's name, type, and description, respectively.

In addition to these methods, an applet typically overrides several of the methods of java.awt.Component, notably the paint() method to draw the applet on the screen.

There are also several Applet methods that you do not override but may call from applet code: showStatus() displays text in the web browser or applet viewer's status line. getImage() and getAudioClip() read image (GIF and JPEG formats) and audio files (AU format) over the network and return corresponding Java objects. getParameter() looks up the value of a parameter specified with a <PARAM> tag within an <APPLET>/</APPLET> pair. getCodeBase() returns the base URL from which the applet's code was loaded, while getDocumentBase() returns the base URL from which the HTML document containing the applet was loaded. Finally, getAppletContext() returns an AppletContext object.

public class Applet extends java.awt.Panel {
// Public Constructors
public Applet ();
// Public Class Methods
1.2public static final AudioClip newAudioClip ( url);
// Property Accessor Methods (by property name)
public boolean isActive (); default:false
public AppletContext getAppletContext ();
public String getAppletInfo (); constant default:null
public getCodeBase ();
public getDocumentBase ();
1.1public java.util.Locale getLocale (); Overrides:Component
public String[ ][ ] getParameterInfo (); constant default:null
// Public Instance Methods
public void destroy (); empty
public AudioClip getAudioClip ( url);
public AudioClip getAudioClip ( url, String name);
public java.awt.Image getImage ( url);
public java.awt.Image getImage ( url, String name);
public String getParameter (String name);
public void init (); empty
public void play ( url);
public void play ( url, String name);
public final void setStub (AppletStub stub);
public void showStatus (String msg);
public void start (); empty
public void stop (); empty
// Public Methods Overriding Component
public void resize (java.awt.Dimension d);
public void resize (int width, int height);

Hierarchy: Object-->Component(java.awt.image.ImageObserver,java.awt.MenuContainer,Serializable)-->Container-->java.awt.Panel-->Applet

Subclasses: javax.swing.JApplet

Passed To: java.beans.AppletInitializer.{activate(), initialize()}, org.omg.CORBA.ORB.{init(), set_parameters()}

Returned By: AppletContext.getApplet()

AppletContextJava 1.0

This interface defines the methods that allow an applet to interact with the context in which it runs (usually a web browser or an applet viewer). The getAppletContext() method of Applet returns an object that implements the AppletContext interface. You can use an AppletContext to take advantage of a web browser's cache or display a message to the user in the web browser or applet viewer's message area. The getAudioClip() and getImage() methods may make use of a web browser's caching mechanism. showDocument() and showStatus() give an applet a small measure of control over the appearance of the browser or applet viewer. The getApplet() and getApplets() methods allow an applet to find out what other applets are running at the same time.

public abstract interface AppletContext {
// Public Instance Methods
public abstract Applet getApplet (String name);
public abstract java.util.Enumeration getApplets ();
public abstract AudioClip getAudioClip ( url);
public abstract java.awt.Image getImage ( url);
public abstract void showDocument ( url);
public abstract void showDocument ( url, String target);
public abstract void showStatus (String status);

Returned By: Applet.getAppletContext(), AppletStub.getAppletContext()

AppletStubJava 1.0

This is an internal interface used when implementing an applet viewer.

public abstract interface AppletStub {
// Property Accessor Methods (by property name)
public abstract boolean isActive ();
public abstract AppletContext getAppletContext ();
public abstract getCodeBase ();
public abstract getDocumentBase ();
// Public Instance Methods
public abstract void appletResize (int width, int height);
public abstract String getParameter (String name);

Passed To: Applet.setStub()

AudioClipJava 1.0

This interface describes the essential methods that an audio clip must have. The getAudioClip() methods of Applet and AppletContext both return an object that implements this interface. The current Java SDK implementation of this interface works only with sounds encoded in AU format. The AudioClip interface is in the java.applet package only because there is no better place for it.

public abstract interface AudioClip {
// Public Instance Methods
public abstract void loop ();
public abstract void play ();
public abstract void stop ();

Returned By: Applet.{getAudioClip(), newAudioClip()}, AppletContext.getAudioClip()

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.