Section 12.4. Java Applets


12.4. Java Applets

Java is an object-oriented programming language developed by Sun Microsystems. It is not related to JavaScript, which is a scripting language developed by Netscape 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 web content, 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 an image. Java applets can be used for all sorts of interactive and multimedia gadgets, such as clocks, calculators, spreadsheets, scrolling marquees, games, text effects, and digital "guitars," just to name a few.

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 (applets take a long time to initialize and tend to crash browsers) and the dominance of Flash for multimedia and interactivity.

12.4.1. 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 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 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.4.2. Adding an Applet to a Page

There are currently two methods for adding an applet to a web page: the object element, recommended by HTML 4.01, and the better supported, though deprecated, applet element.

applet

 <applet>...</applet> 

This element is deprecated.

Attributes

Core (id, class, style, title)
alt="text"
archive="URLs"
code="class" (Required)
codebase="URL"
height="number"
name="text"
object="text"
width="number"

Deprecated attributes

align="left|right|top|middle|bottom"
hspace="number"
vspace="number"

The W3C has deprecated the applet element and all its attributes in favor of the object element. Despite this, the applet element 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 applet be used, so read the documentation for the applet first. This section looks at both methods.

12.4.2.1. Adding applets with applet

The applet element is a container for any number of parameter (param) elements. The following is an example of how an applet element 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 applet element 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 element may also use many of the same attributes used for images, such as alt for providing alternative text if the applet cannot be displayed, and presentational attributes such as 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 elements (sometimes there are none). The param element 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.

12.4.2.2. Adding applets with object

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

 <object 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 element 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 element may contain a number of parameter (param) elements, as with the applet element. (Note that Netscape 4.0 does not support param elements within object, so it may not play applets correctly if placed this way.)




Web Design in a Nutshell
Web Design in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009879
EAN: 2147483647
Year: 2006
Pages: 325

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net