Introduction to Java : Building Your First Application part-2

Writing Applets

Applets are programs that run inside of a web page using the resources the web browser has to offer such as a Java Virtual Machine (JVM) and the default set of class libraries.

When you initially hit a web page with an incorporated Java applet, you usually see a gray box where the applet will show up and then slowly, the applet loads and appears in the space of the gray box.

In addition to appearing in a web page, applets can appear in their own windows. That is, they can appear within their own application frame that will popup from the web browser’s window. This frame can be moved and iconified separately from the web browser’s window.

However, applets that appear in their own window typically display a message like, “Warning: Applet Window” in the bottom of their frame. This tells the user that the frame is part of a Java applet running from inside the browser. The web browser designers added this feature to prevent an applet from masquerading as some other window, such as a system password entry window or as some other application.

The AWT class java.awt.Applet defines an applet. This class provides all of the basic features and methods which makeup an applet object.

Applet Lifecycle

An applet begins its life when the web browser loads its classes and calls its init() method. Thus, in the init() method you should provide initialization code such as the initialization of variables. Once the initialization is complete, the web browser will call the start() method in the applet. At this point the user can begin interacting with the applet.

But what would happen if the user moved to another web page while the applet was executing? Well, if this happens, the web browser will call the applet’s stop() method so that the applet can take a breather while the user goes off and explores the web some more.

If the user returns to the applet, the web browser will simply call the applet’s start() method again and the user will be back into the program.

Finally, if the user decides to quit the web browser, the web browser will free up system resources by killing the applet before it closes. To do so, it will call the applets destroy() method.

You are welcome to override any of the methods in order to provide your own logic. For example, you may want to provide logic in the stop() method which performs some cleanup or save operation.

Finally, you can override destroy() to perform one-time tasks upon program completion. One example is cleaning up threads which were started in the init() method.

Applets by Example

Okay, we still have a few more things to talk about before you actually build your own applet. However, let’s look at a simple applet so you can see what it would look like. Notice that we have to import the applet package.

Applet HTML

After creating the Java code for an applet, you will need to compile it to create a Java .class files used to run the program. If you are using the JDK, the following command can be used to compile the applet (as you can see, it is just the same as for applications):

Make sure that you are in the same directory as the .java file you are compiling and make sure that you are careful to type the name of the .java file correctly.

When you compile the .java file, you should have a Hello.class file in the same directory as the .java file.

Now, you are ready to distribute your applet over the web. To do so, you must create an HTML page that displays the applet using the <APPLET></APPLET> HTML tag to define an applet which you would like the browser to run.

When a web browser interprets an HTML page sent to it from a web server, it begins decoding it right away. In other words, each tag that the browser encounters is read and interpreted. When the browser reaches the <APPLET> tag, it either ignores it if it does not understand Java, or it sends a request to the server to retrieve the applet code in the location specified by the tag.

For example, the following code provides an example of what an <APPLET> tag might look like in the body of an HTML page:

The following table goes over the attributes in the APPLET tag.

CODESpecifies the name of the class file which starts your applet.
CODEBASESpecifies the base directory on the web server where your class files are stored.
ALTSpecifies the text which should be displayed if the browser is capable of running Java applets but is unable to run your applet.
NAMESpecifies the name of your aplet within the context of the HTML page and other applets which might be running on the same page.
WIDTH and HEIGHTSpecifies the amount of space given for your applet in pixels.
ALIGNSpecifies how your applet will be aligned within the browser window. You can pass the following values along with this attribute: TOP, BOTTOM, LEFT, RIGHT, TEXTTOP, MIDDLE, ABSMIDDLE, BASELINE, BOTTOM. These values are standard HTML values such as those for images. If you need an explanation check out an HTMl reference guide.
VSPACE and HSPACESpecifiy how much of a margin to give the applet in pixels.