The APPLET tag of HTML is used to start an applet either from a web browser or an applet viewer. The HTML tag allows a Java applet to be embedded in an HTML document.
A number of optional attributes can be used to control an applet’s appearance, for example, the dimensions of the applet panel. Also, parameters can be independently defined which can be passed to the applet to control its behavior.
The complete syntax of the applet tag is given below:
<applet
code = “appletFile” -or- [object = “serializedApplet”]
width = “pixels”
height = “pixels” [codebase = “codebaseURL”]
[archive = “archiveList”]
[alt = “alternateText”]
[name = “appletlnstanceName”]
[align = “alignment”]
[vspace = “pixels”]
[hspace = “pixels”]
>
[<param name = “appletAttribute1” value = “value”>]
[<param name = “appletAttribute2” value = “value”>]
….
….
[alternateHTML]
</applet>
We’ll be covering the following topics in this tutorial:
Attributes in the applet tag.
The attributes code, width and height are mandatory and all other attributes are options, which are shown in brackets ([]). The attributes need not follow the order that was specified in the above syntax. The description of each attribute is given below:
codeThe name of the Java applet. Class file, which contains the compiled applet code. This name must be relative to the base URL of the Applet and cannot be an absolute URL. The extension in the file name is optional.
width This refers to the width of the applet panel specified in pixels in the browser window.
height This refers to the height of the applet panel specified in pixels in the browser window. The width and height attributes specify the applet’s, display area. This applet area does not include any windows or dialogue boxes that the applet shows.
Codebase This refers to the base URL of the applet. That is, the URL of the directory that contains the applet class file. If this attribute is not specified then the HTML document’s URL directory is taken as the CODEBASE.
archive There are one or more archive files containing Java classes and other resources that will be preloaded. The classes are loaded using an instance of the AppletClassLoader class with the given codebase. The archives in archivelist are separated by a comma (,).
alt This refers to text to be displayed if the browser understands the applet tag but cannot run Java applets.
name This refers to a name for the applet instance which makes it possible for applets to
communicate with each other on the same HTML page. The getApplet () method, which is defined in the AppletContext interface, can be used to get the applet instance using name.
align This refers to the alignment of the apple!. The possible attribute values are LEFT, RlGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE and ABSBOTTOM.
vspace This refers to the space, specified in number of pixels, used as a margin above and below the applet.
hspace This refers to the space, specified in number of pixels, used as a margin to the left and right of the applet.
param The param tag, specifies an applet parameter as a name-value pair. The name is the parameters name, value is. its value. The values of these parameters can be obtained using the getParameter () method.
alternateHTML This is ordinary HTML to be displayed in the absence of Java or if the browser does not understand the applet tag. This is different from the alt attribute which understands the· applet tag but cannot run, whereas the alternateHTML tag is provided when a browser does not support applets.
Passing parameters to applets
The applet tag helps to define the parameters that are to be passed to the applets. Programs illustrate the method of passing parameters to an applet. In this example the first parameter that is presented contains text whose location is specified by the other two parameters. Program is the HTML code for this.
Program HTML code for the applet program for passing parameters.
<! DOCTYPE HTML PUBLIC“-//W3C//DTD HTML4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE>Example for Parameter passing to applet </TITLE>
</HEAD>
<BODY>
<H3>This is an example for passing parameters to Applet</H3> <p>
The parameters are<p>
text= Text Parameter <p>
x = 50 <p>
y = 50 <p>
<H5> THE TEXT PARAMETER WILL BE DISPLAYEDIN THE APPLETAT THE
X, Y LOCATION </H5>
<APPLETCODE = “ParamApplet.c1ass” WIDTH = 300 HEIGHT = 100>
<PARAMNAME = “text” VALUE = “Text Parameter”>
<PARAM NAME = “x” VALUE = “50”>
<PARAM NAME = “y” VALUE = “50”>
</APPLET>
</BODY>
</HTML>
After writing the HTML code let us write the applet program that will retrieve the parameters specified in the HTML tag by using the getParameter () method. This method has one argument, which specifies the name of the argument and returns the value of the argument in the form of a string. The Integer.parselnt () method is used to convert the parameters to the integer datatype from the string datatype.
Program passing parameters to an applet.
import java.applet*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Para Applet extends Applet
{
public void init (){}
public void start (){}
public void destroy (){}
public void stop (){}
public void paint (Graphics g)
{
String text=getParameter (“text”);
g.drawString (text, 50, 50);
}
}