12.6. Java Applets
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.
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.
22.214.171.124. 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>
126.96.36.199. Adding applets with <applet>
<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:
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.
Copyright © 2002 O'Reilly & Associates. All rights reserved.