start page | rating of books | rating of authors | reviews | copyrights

Book HomeWeb Design in a NutshellSearch this book

12.6. Java Applets

Java is an object-oriented programming language developed by Sun Microsystems (http://www.sun.com). It should be noted that it is not related to JavaScript, which is a scripting language developed by Netscape Navigator to run within an HTML document in a browser. Because Java is a full programming language (like C or C++), it can be used to create whole applications.

Java's primary contribution to the Web, however, has been in the form of Java applets, which are self-contained, mini-executable programs. These programs, named with the .class suffix, can be placed right on the web page, like a graphic.

12.6.1. Advantages and Disadvantages

Applets are ideal for web distribution for the following reasons:

Of course, every utopian technology has its darker side, and unfortunately, in the real world, browsers can be temperamental in the way they handle Java applets. Browsers are notorious for crashing in the presence of a computation-hungry applet. In general, it also takes browsers a long time to initialize Java, which tends to chase users away. There was a great buzz among web developers when Java applets first hit the scene, but since then enthusiasm has waned in the face of performance issues and the development of other web multimedia solutions.

12.6.2. What Applets Can Do

What can't applets do?! Java applets are used for everything from simple animations to flight simulators. Because Java allows for computations on-the-fly, they are useful for programs that interact with user input. Not surprisingly, a large percentage of Java applets are games, but applets are also used for more practical purposes, such as calculators and spreadsheets. More interestingly, they can serve live data (news headlines, stock quotes, sports scores, etc.) and let users navigate through complex data relationships.

There are probably thousands of Java applets out there. The following is just a smattering of the types of things they can do:

12.6.3. Where to Get Applets

If you need a customized applet for your site, your best bet is to hire a programmer to create one to your specifications. However, there are a number of applets available for free or for a licensing fee that you can download from libraries on the Web.

A good place to start is the applets section of Sun's Java site at http://java.sun.com/applets/. This page provides a list of links to applet-related resources.

If you are looking for cool applets you can use right away, try the JavaBoutique at http://javaboutique.internet.com. Here you will find hundreds of applets available for download as well as clear instructions for their use. It's a great way to add interactivity to your site without learning any programming.

12.6.4. Downloading and Using Java Applets

In addition to these, there are a number of small businesses with Java applet packages for sale or available for a nominal licensing fee. Because the list is constantly changing, I recommend doing a search for "Java Applets" on Yahoo (http://www.yahoo.com) or your favorite search engine.

It is fairly easy to download an applet and add it to a web page. The steps below follow the instructions provided by the JavaBoutique for downloading applets from their site, but they can be used for applets from any resource.

  1. Download the .class file along with any associated image or audio files. (Note that there is a bug in Navigator 4.0 that requires you to hold the Shift key before clicking the link for the .class file.) In some cases, you may be given the raw Java code, in which case you need to compile it using Sun's Java Developer Kit.

  2. The .class file should be saved in the same directory as the HTML file, unless otherwise noted by the codebase attribute in the associated <applet> tag (this attribute gives the path for the applet). If the applet requires additional resources (such as image or audio files), be sure to save them in the same directory structure you found them (or follow the directions provided with the applet).

  3. When getting an applet from a library such as JavaBoutique, the required HTML source is made available with the download, so you can just copy and paste it into your HTML document and adjust the parameters as necessary.

  4. Test the applet in a browser or applet viewer. Because applets run client-side, you don't need a server to do your testing. Most problems with applets are due to elements not being in the right places. Make sure that your .class file is in the directory noted by the codebase attribute or in the same directory as the HTML file if no codebase is specified. Also be sure that your supporting resource files are in their correct directories and that everything is named correctly (remember that names in Java code are case-sensitive). Troubles may arise in setting all the parameters correctly, but these problems cannot be anticipated and need to be solved on a per-applet basis.

  5. Last but not least, it is good form to credit the author of the applet as well as the online resource. The JavaBoutique provides a discreet logo you can place on the page with the applet.

12.6.5. Adding an Applet to a Page

There are currently two methods for adding an applet to a web page: the <object> tag, recommended by HTML 4.01, and the better-supported <applet> tag.

The W3C has deprecated the <applet> tag and all its attributes in favor of the <object> tag. Despite this, the <applet> tag may still be the better choice, because browser support for <object>-embedded applets is so inconsistent that it is difficult to find an approach that works in all browsers. In addition, some applets require that the <applet> tag be used, so read the documentation for the applet first. This section looks at both methods.

12.6.5.1. Adding applets with <object>

You can add a simple, self-contained applet to an HTML document using the <object> tag like this:

<OBJECT CLASSID="applet.class" CODEBASE="http://somedomain.com/classes/">
An applet with some useful function should display in this space.
</OBJECT>

The classid attribute points to the applet itself (its implementation). It has the same function as the code attribute in the <applet> tag when used for Java applets. classid may not contain any pathname information, so the location of the class file is provided by the codebase attribute.

When using <object> for Java applets, the object tag may contain a number of parameter (<param>) tags, as with the <applet> tag. (Note that Netscape 4.0 does not support <param> tags within the <object> tag, so it may not play applets correctly if placed this way.)

The following is an example of an applet with additional parameters:

<OBJECT CLASSID="applet.class" CODEBASE="http://somedomain.com/classes/">
  <PARAM NAME="param1" VALUE="value1">
  <PARAM NAME="param2" VALUE="value2">
  <PARAM NAME="param3" VALUE="value3">
An applet with some useful function should display in this space.
</OBJECT>

12.6.5.2. Adding applets with <applet>

The <applet> tag is a container for any number of parameter (<param>) tags. The following is an example of how an <applet> tag for a game might look:

<APPLET CODEBASE=class CODE="Wacky.class" WIDTH=300 HEIGHT=400>
<PARAM NAME="Delay" VALUE="250">
<PARAM NAME="Time" VALUE="120">
<PARAM NAME="PlaySounds" VALUE="YES">
</APPLET>

The opening applet tag contains a number of standard attributes:

code

Tells the browser which applet will be used. Applets end with the suffix .class or .jar. This attribute is required.

codebase

This tells the browser in which directory to find the applets. If the applets are in the same directory as the page, the codebase attribute is not necessary.

width, height

These specify the pixel dimensions of the "window" the applet will occupy. These attributes are required for the Java applet to function properly.

The <applet> tag can also take many of the same attributes used for images, such as alt (for providing alternative text if the applet can not be displayed), align (for positioning the applet in the flow of text), and hspace/vspace (used in conjunction with align).

Special parameters for the applet are provided by any number of parameter tags (sometimes there are none). The <param> tag always contains the name of the parameter (name=) and its value (value=). Parameters provide special settings and controls that are specific to the particular applet, so you need to follow the parameter coding instructions provided by the programmer of the applet.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.