Chapter 27

Package java.applet


CONTENTS


The java.applet package contains the Applet class as well as three interfaces: AppletContext, AppletStub, and AudioClip. The java.applet package's class and interfaces are described in detail throughout this chapter.

Applet

Panel

The class hierarchy for the Applet class derives from class java.awt.Panel. This implies that every applet has some visual component. The basic visual component is a panel in an HTML page. Applet's overall derivation looks like Figure 27.1.

Figure 27.1: Derivation of java.applet.Applet

Listing 27.1 shows the declarations for all of the public methods included in the java.applet.Applet class.


Listing 27.1. Public members of java.applet.Applet.
public class Applet extends Panel {
    public final void setStub(AppletStub stub)
    public boolean isActive()
    public URL getDocumentBase()
    public URL getCodeBase()
    public String getParameter(String name)
    public AppletContext getAppletContext()
    public void resize(int width, int height)
    public void resize(Dimension d)
    public void showStatus(String msg)
    public Image getImage(URL url)
    public Image getImage(URL url, String name)
    public AudioClip getAudioClip(URL url)
    public AudioClip getAudioClip(URL url, String name)
    public String getAppletInfo()
    public String[][] getParameterInfo()
    public void play(URL url)
    public void play(URL url, String name)
    public void init()
    public void start()
    public void stop()
    public void destroy()
}

Applet

Applet

public Applet()

This is the default constructor for the Applet class. This function creates a new Applet. Each applet should implement at a minimum the init() or start() method to display itself on the screen and for initial setup.

SetStub

Applet

public final void setStub(AppletStub stub)

setStub sets the AppletStub to the stub passed in. This function is called automatically by the underlying system and usually is not called directly. The only time you need to implement AppletStub methods is if you are writing your own applet viewer or browser.

stub is the underlying stub used to implement an applet viewer.

IsActive

Applet

public boolean isActive()

isActive is used to determine whether this applet is currently active. An applet is set Active just before the Start method is called (see start() later in this chapter).

getDocumentBase

Applet

public URL getDocumentBase()

getDocumentBase returns the URL of the current page that this applet is embedded in.

A URL object containing information about the current URL. (See the java.net.URL class documentation in Chapter 33, "Package java.net.")

getCodeBase

Applet

public URL getCodeBase()

getCodeBase returns the URL of the applet itself.

A URL object (see the java.net.URL class documentation in Chapter 33) containing information about the applet's URL.

getParameter

Applet

public String getParameter(String name)

getParameter returns the String value of the parameter passed in using the HTML <PARAM> tag (for more information on parameters, see Chapter 12, "HTML for Java Programmers").

Here's an HTML example:

<APPLET CODE="CoolApplet.class" WIDTH=150 HEIGHT=30>
<PARAM NAME=COLOR VALUE="WHITE">
</APPLET>

Here's a Java example:

tempString = getParameter("COLOR");
if (tempString.equals("WHITE"))
  tempColor = new Color(255, 255, 255);

name is a case-sensitive string that matches (exactly!) the parameter name passed in using the HTML PARAM tag.
A String value representing the PARAM tag's VALUE attribute.

getAppletContext

Applet

public AppletContext getAppletContext()

getAppletContext returns an AppletContext object. This object can be used to determine information about the applet's runtime environment.

An AppletContext object (see documentation on the AppletContext interface later in this chapter).

resize

Applet

public void resize(int width, int height)

resize makes use of the Applet class's inheritance from the Panel class to resize the applet based on the input values.

width is an integer value specifying the applet's new width.

height is an integer value specifying the applet's new height.

resize

Applet

public void resize(Dimension d)

This resize function accepts a Dimension object as its argument. (For more information on the Dimension class, see the documentation on the java.awt.Dimension class in Chapter 28, "Package java.awt.")

d is a Dimension object that specifies the new size of the applet.

showStatus

Applet

public void showStatus(String msg)

showStatus shows a status message using the applet's context.

msg is a string containing the message to be displayed.

getImage

Applet

public Image getImage(URL url)

getImage retrieves image information based on the URL input parameter. Note that this function returns immediately and does not retrieve the entire image. This image will not be retrieved until the Image object is actually needed.

url is a URL object containing location information for the image to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

An Image object containing information about the URL passed in. For more information on the Image class, see the java.awt.Image documentation in Chapter 28.

getImage

Applet

public Image getImage(URL url, String name)

This getImage function accepts both the URL input parameter containing base location information as well as a String input parameter containing the filename. This function is useful if, for instance, the base URL of the applet is unknown. The applet could then call:

theImage = getImage(getDocumentBase(), "file.jpg");

Note that this function returns immediately and does not retrieve the entire image. This image will not be retrieved until the Image object is actually needed.

url is a URL object containing base location information for the image to be retrieved (such as http://www.javasoft.com). For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

name is a String object containing a filename relative to the base URL passed using the URL argument.

An Image object containing information about the URL passed in. For more information on the Image class, see the java.awt.Image documentation in Chapter 28.

getAudioClip

Applet

public AudioClip getAudioClip(URL url)

getAudioClip retrieves an AudioClip based on the URL input parameter.

url is a URL object containing location information for the clip to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

An AudioClip object that can be played at a later time. For more information on the AudioClip interface, see the java.applet.AudioClip documentation later in this chapter.

getAudioClip

Applet

public AudioClip getAudioClip(URL url, String name)

This getAudioClip function accepts both the URL input parameter containing base location information as well as a String input parameter containing the filename. This function is useful if, for instance, the base URL of the applet is unknown. The applet could then call:

RocknRoll = getAudioClip(getDocumentBase(), "paul_westerberg.au");

url is a URL object containing base location information for the AudioClip to be retrieved (such as http://www.sony.com). For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

name is a String object containing a filename relative to the base URL passed using the url argument.

An AudioClip object that can be played at a later time. For more information on the AudioClip interface, see the java.applet.AudioClip documentation later in this chapter.

getAppletInfo

Applet

public String getAppletInfo()

getAppletInfo is provided for applet authors to return name, copyright, and version information for their applets. The default implementation returns null.

A String containing author, version, and copyright information (or anything else) for the applet.

getParameterInfo

Applet

public String[][] getParameterInfo()

getParameterInfo is provided for applet authors to provide information on any parameters that the applet may take as input. Conventional return values have the following information: name, type, and comments. The default implementation returns a null String array.

A String array where each element contains, by Java conventions, three values: name, type, and comments. Each of these elements represents a parameter that the applet takes as input.

play

Applet

public void play(URL url)

play is used to play an AudioClip at the location given by the URL input parameter. For more information on the AudioClip interface, see the documentation on the java.applet.AudioClip interface later in this chapter.

url is a URL object containing location information for the clip to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

play

Applet

public void play(URL url, String name)

This play method is used to play an AudioClip given a base URL and a filename for input parameters. For more information on the AudioClip interface, see the documentation on the java.applet.AudioClip interface later in this chapter.

url is a URL object containing base location information for the AudioClip to be retrieved (such as http://www.sony.com). For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

name is a String object containing a filename relative to the base URL passed using the URL argument.

init

Applet

public void init()

The init method is called automatically after the applet is created. This function never needs to be called directly.

start

Applet

public void start()

The start method is called automatically to start the applet after it has been initialized. This function never needs to be called directly. Start is called when an applet is first displayed on a screen, or when a page is revisited within a Web browser.

stop

Applet

public void stop()

The stop method is called automatically to stop an applet from running. This function never needs to be called directly unless the applet knows that it needs to stop executing. Stop is called when the Web page containing the applet is replaced by another Web page.

destroy

Applet

public void destroy()

The destroy method is called automatically when the applet's system resources are being reclaimed. This function never needs to be called directly. Destroy is called after the Stop method has finished.

AppletContext

The AppletContext interface is provided to give information on an applet's environment. An AppletContext interface can be obtained by calling the Applet class's getAppletContext() method.

Listing 27.2 shows the declarations for all of the public methods included in the java.applet.AppletContext class.


Listing 27.2. Public members of java.applet.AppletContext.
public interface AppletContext {
  public abstract AudioClip getAudioClip(URL url)
  public abstract Image getImage(URL url)
  public abstract Applet getApplet(String name)
  public abstract Enumeration getApplets()
  public abstract void showDocument(URL url)
  public abstract void showDocument(URL url, String target)
  public abstract void showStatus(String status)
}

getAudioClip

AppletContext

public abstract AudioClip getAudioClip(URL url)

getAudioClip retrieves an AudioClip based on the URL input parameter.

url is a URL object containing location information for the clip to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

An AudioClip object that can be played at a later time. For more information on the AudioClip interface, see the java.applet.AudioClip documentation later in this chapter.

getImage

AppletContext

public abstract Image getImage(URL url)

getImage retrieves image information based on the URL input parameter. Note that this function returns immediately and does not retrieve the entire image. This image will not be retrieved until the Image object is actually needed.

url is a URL object containing location information for the image to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

An Image object containing information about the URL passed in. For more information on the Image class, see the java.awt.Image documentation in Chapter 28.

getApplet

AppletContext

public abstract Applet getApplet(String name)

getApplet returns an applet from the current AppletContext based on the input name argument.

name is a String object representing an applet's name. This name should correspond to the applet's HTML name attribute.

An Applet object or null if no applet exists with the designated name.

getApplets

AppletContext

public abstract Enumeration getApplets()

getApplets returns an Enumeration interface for all of the applets on the current AppletContext.

An Enumeration interface that can be used to retrieve all of the applets on the current applet context. For more information on the Enumeration interface, see the documentation for java.util.Enumeration in Chapter 34, "Package java.util."

showDocument

AppletContext

public abstract void showDocument(URL url)

showDocument loads the URL argument into the current AppletContext if it is a valid URL. This method may be ignored, depending on the applet context.

url is a URL object containing location information for the image to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

showDocument

AppletContext

public abstract void showDocument(URL url, String target)

This showDocument method loads the URL argument into a target window or frame depending on the target string. This method may be ignored, depending on the applet context.

url is a URL object containing location information for the image to be retrieved. For more information on the URL object, see the java.net.URL class documentation in Chapter 33.

target-The target string can be one of the following values:

showStatus

AppletContext

public abstract void showStatus(String status)

showStatus shows a status message using the applet's context.

msg is a string containing the message to be displayed.

AppletStub

The java.applet.AppletStub interface is most often used to build applet viewers, browsers, or other tools that want to display applets within them. This interface is not normally implemented by Java applet developers.

Listing 27.3 shows the declarations for all of the public methods included in the java.applet.AppletStub class.


Listing 27.3. Public members of java.applet.AppletStub.
public interface AppletStub {
  public abstract boolean isActive()
  public abstract URL getDocumentBase()
  public abstract URL getCodeBase()
  public abstract String getParameter(String name)
  public abstract AppletContext getAppletContext()
  public abstract void appletResize(int width, int height)
}

isActive

AppletStub

public abstract boolean isActive()

isActive is used to determine whether this applet is currently active.

isActive returns true if the applet is active; false if not.

getDocumentBase

AppletStub

public abstract URL getDocumentBase()

getDocumentBase returns the URL of the current page that this applet is embedded in.

A URL object (see the java.net.URL class documentation in Chapter 33) containing information about the current URL.

getCodeBase

AppletStub

public abstract URL getCodeBase()

getCodeBase returns the URL of the applet itself.

A URL object (see the java.net.URL class documentation in Chapter 33) containing information about the applet's URL.

getParameter

AppletStub

public abstract String getParameter(String name)

getParameter returns the String value of the parameter passed in using the HTML <PARAM> tag. (For more information on parameters, see Chapter 12.)

getAppletContext

AppletStub

public abstract AppletContext getAppletContext()

getAppletContext returns an AppletContext object. This object can be used to determine information about the applet's runtime environment.

An AppletContext object (see documentation on the AppletContext interface earlier in this chapter).

appletResize

AppletStub

public abstract void appletResize(int width, int height)

appletResize is called when the applet wants to be resized.

width is an integer value specifying the applet's new width.

height is an integer value specifying the applet's new height.

AudioClip

The AudioClip interface is used to provide high-level access to sound playback capabilities. This interface, like AppletContext and AppletStub, is usually only implemented by applet viewers.

Listing 27.4 shows the declarations for all of the public methods included in the java.applet.AudioClip class.


Listing 27.4. Public members of java.applet.AudioClip.
public interface AudioClip {
  public abstract void play()
  public abstract void loop()
  public abstract void stop()
}

play

AudioClip

public abstract void play()

The play method plays audio files from the beginning until the end or stop method is called.

loop

AudioClip

public abstract void loop()

The loop method plays audio files in a loop continuously.

stop

AudioClip

public abstract void stop()

The stop method stops the playing of an audio file.