Chapter 12

The Applet Package

by Debasis Samanta


CONTENTS

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();	 
	}

The methods in this class are described in Table 1.1.

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 method to create the threads and the destroy method to kill them.

getAppletContext()

Determines this applet's context, which allows the applet to query and affect the environment in which it runs. This environment of an applet represents the document that contains the applet.

getAppletInfo()

Returns information about this applet. An applet should override this method to return a String containing information about the author, version, and copyright of the applet.

getAudioClip(URL url)

Returns the AudioClip object 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 name)

Returns the AudioClip object specified by the URL and name arguments.


Table 1.1 (Contd.)

 

getCodeBase()

Returns : The URL of this applet.

getDocumentBase()

Returns : The URL 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

getImage(URL url, String name)

Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument.

This method always returns immediately, whether or not the image exists. When this 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.

getParameter(String name)

Returns the value of the named parameter in the HTML tag. For example, if this applet is specified as :

<applet code="Clock" width=50 height=50>

<param name= Color value ="blue">
</applet>

A call to getParameter("Color") returns the value "blue". The name argument is case insensitive.

getParameterInfo()

Returns information about the parameters than are understood by this applet. An applet should override this method to return an array of Strings describing these parameters.

init()

This method is called by the browser or applet viewer to inform this applet that it has been loaded into the system. It is always called before the first time that the start method is called. A subclass of Applet should override this method if it has initialization to perform. For example, an applet with threads would use the init method to create the threads and the destroy method to kill them.

isActive()

Determines if this applet is active. An applet is marked active just before its start method is called. It becomes inactive immediately after its stop method is called.

Returns : true if the applet is active; false otherwise.

play(URL url)

Plays the audio clip at the specified absolute URL. Nothing happens if the audio clip cannot be found.


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.

resize(Dimension d)

Requests that this applet be resized with the specified dimension.

resize(int width, int height)

Requests that this applet be resized with the specified parameters.

setStub(AppletStub stub)

Sets this applet's stub. This is done by automatically by the system.

showStatus(String msg)

Requests that the argument string be displayed in the "status window." Many browsers and applet viewers provide such a "status window" where the application can inform users of its current state.

start()

This method is called by the browser or applet viewer to inform this applet that it should start its execution. It is called after the init method and each time the applet is revisited in a Web page. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is visited. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation.

stop()

This method is called by the browser or applet viewer to inform this applet that it should stop its execution. It is called when the Web page that contains this applet has been replaced by another page and also just before the applet is to be destroyed. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is no longer visible. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation.

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.

   public  interface  java.applet.AppletContext
	{
        	// 	Methods
    	public abstract Applet getApplet(String  name);	
    	public abstract Enumeration getApplets();	 
    	ublic abstract AudioClip getAudioClip(URL  url);	 
    	public abstract Image getImage(URL  url);	 
    	public abstract void showDocument(URL  url);	 
    	public abstract void showDocument(URL  url, String  target);
    
	public abstract void showStatus(String  status); 
	}
    Methods are stated in the Table 1.2.

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 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 target)

Requests that the browser or applet viewer show the Web 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.

    public  interface  java.applet.AppletStub
	  {
        	// 	Methods
    	public abstract void appletResize(int  width, int  height);	 
    	public abstract AppletContext getAppletContext(); 
    	public abstract URL getCodeBase();	 
    	public abstract URL getDocumentBase();	 
    	public abstract String getParameter(String  name); 
    	public abstract boolean isActive();	 
	  }

    Methods are described in Table 1.3

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 is called. It becomes inactive immediately after its stop 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.

	public  interface  java.applet.AudioClip
	{
        // Methods
    	public abstract void loop(); 
    	public abstract void play(); 
    	public abstract void stop();
	}

	Methods are described in Table 1.4.

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.