Writing and Running the Java Applet

1. WRITING AN APPLET

Let us now write our first applet called HeiioWorid which will display a simple message “Hello World!” on the screen. The following is the source code of the HeiioWorid applet stored in the file

HelloWorld.java.

//HelloWorld.java

public class HelloWorld extends java.applet.Applet

{

public void paint(java.awt.Graphics g)

{

g.drawString(“Hello World!”, 20, 20);

}

}

Your applet must extend either the java.appiet.Appiet or the javax.swing.JAppiet class. In this example, the class HeiioWorid extends the Appiet class and hence is an applet. It overrides only the paint() method to display a message on the screen. Since this applet is not supposed to do anything except displaying the message “Hello Word!”, we have not overridden any other method. As mentioned earlier, the paint() method takes a Graphics object that represents the applet window. Numerous methods are available on this Graphics object, to display text, images and graphic objects (such as circle, ellipse, line, and rectangle) on the screen. In this case, we have used the drawString() method, which takes three arguments: a string to be displayed on the screen and two integer arguments, which represent the x and y coordinates, where the string is displayed .

2. GENERATING CLASS FILE

Generating class file for an applet is exactly same as generating class files from application programs. The same javac compiler bundled with the Java Development Kit (JDK) is used. To compile our HeiioWorid applet, go to the directory containing the file HelloWorld.java and use the following command:

javac HelloWorld.java

On successful compilation, it creates a class file named HeiioWorid.ciass, which contains the byte code for our HeiioWorid applet. Make sure that the directory containing the javac compiler is included in your path environment variable.

3. RUNNING THE APPLET

Running an applet is different from running a Java application program. To run an applet, its class file has to be embedded in an html file using <appiet> tag. The following section describes how to embed an applet within an html file.

3.1. The Applet Tag

Two HTML tags are used to work with applets: <appiet> and <param>. An applet is typically embedded in an HTML document using the <appiet> tag. The <param> tag is used to pass parameters to an applet. Some browsers provide the <object> or <embed> tag to include an applet. For more information, please see the documentation of your browser. The <appiet> tag instructs the browser that an applet has to be loaded and executed using the Java Runtime Environment (JRE). It takes three mandatory attributes: code, width, and height. The code attribute specifies the class file for this applet to be instantiated. The width and height attributes indicate the width and height, respectively, of the applet window to be created. For our HeiioWorid applet, we create a simple HTML file,

HelloWorld.html, as follows:

<!– HelloWorld.html –>

<html>

<head><title>Applet Demo</title></head>

<body>

<applet code=”HelloWorld” height=50 width=150 >

</applet>

</body>

</html>

Now, open it in a browser. If the browser is Java-enabled, it understands the meaning of the <applet> tag. It loads the class file HelloWorld.class and executes it and places the result at the place of the <applet> tag. Note that the extension .class is not used in the <applet> tag. Put the HelloWorld.class file in the same directory as the HelloWorld.html file. The result is shown in Figure 16.9:

If you do not see the applet running, make sure that at least the Java 2 Standard Edition (J2SE) platform is installed in your client computer and linked with the browsers. Most often the browsers have their own Java Runtime Environment (JRE) to interpret applets. Alternatively, you can use the application appletviewer, which comes with JDK, as follows.

appletviewer HelloWorld.html

The output [Figure 16.10:] now looks exactly the same except that it creates a window with a different appearance.

The appletviewer program comes with JDK and allows us to run applets outside a web browser. It displays applets embedded in the specified document (using <applet>, <object> or <embed> tags)

in its own window. Note that the application appietviewer cannot download the class file from the remote web server. It is usually used for testing applets before deploying them to a web server. The appietviewer program is usually kept in the bin subdirectory of Java’s installation directory. To use this program from any directory, make sure that the directory containing the appietviewer program is included in your path environment variable.

Though appietviewer functions like a web browser, the way it functions is different. It processes only applet tag (if any) embedded in a document. So, the document containing applets need not always be an html document. Sometimes, for quick development and to avoid creating a separate html file, the applet is embedded in the applet source file (.java file). For example, we can embed our HelloWorld applet in the HeiioWorid.java file as follows:

/*

<applet code=”HelloWorld” height=50 width=300>

</applet>

*/

public class HelloWorld extends java.applet.Applet

{

public void paint(java.awt.Graphics g)

{

g.drawString(”Hello World!”, 20, 20);

}

}

We can now pass this document to the appietviewer program. Since, the <appiet> tag (which is not a valid Java code) is inserted in the Java source file, which is compiled using javac, the tag is written as comments to avoid compilation error. As mentioned earlier, the appietviewer program looks at only <appiet> tag ignoring everything else. Consequently, it is expected to display our applet in its windows. The following command may be used to view the applet.

appietviewer HeiioWorid.java

The result is exactly same as shown in Figure 16.10:

If you want to deploy the applet on a web server, place these two files in the same folder. The result is shown in Figure 16.12:

Figure 16.12: Result of HelloWorld applet. The applet was deployed on tomcat web
server running in a computer having IP address 172.16.5.81

The <appiet> tag takes several other optional attributes. It takes the following general form:

<applet

code=’appletClassFile’

[codebase=’URLOfCodeBase’]

[alt=’altervativeText’]

[name=’NameOfApplet’]

width=’width of Applet window in pixels’

height=’height of Applet window in pixels’

[align=’alignment’]

[vspace=’pixels’]

[hspace=’pixels’]>

The optional attributes are shown in square brackets. Let us discuss the functionality of each of these attributes.

code — This attribute specifies the name of the class file (without the extension .class) that contains the applet’s byte code.

codebase — It specifies the base URL to be searched for the applet’s executable class file. If nothing is mentioned, the directory from where the HTML document containing this applet was downloaded is assumed. In such cases, you must put the applet’s class file and the HTML document in the same directory. However, different URLs may also be used with some restrictions.

alt — It specifies a message to be displayed if the browser has been able to understand the applet, but has failed to execute it for some reason such as the user turning off the applet.

name — This attribute specifies a name for an applet instance. Other applets use this name to find it and communicate with it.

width — It is the width of the applet window in pixels. height — It is the height of the applet window in pixels.

align — It is used to adjust the position of an applet with respect to the surrounding text and images. It can have the following values:

  • left — It puts the applet on the left side of the page and causes text to wrap around it.
  • right — It puts the applet on the right side of the page and causes text to wrap around it.
  • top — It aligns the top of the applet with the top of the text.
  • bottom — It aligns the bottom of the applet with the bottom of the text.
  • middle — This value works differently in different browsers. In some browsers, it aligns the middle of the text with the middle of the applet. In some other browsers, it aligns the bottom of the text with the middle of the applet window.
  • baseline — It aligns the bottom of the applet with the baseline of the text. The baseline is the bottom line of characters such as a, b, c, d, and e. Some letters such as g, j, and p dangle below this baseline.

vspace — It specifies the space to be left above and below the applet window, in pixels.

hspace — It specifies the space to be left on the left and right of the applet window, in pixels.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *