Java™ Applet Basics
What Is A Java™ Applet?
An applet is one or more .class files which together form a program. Unlike other types of program, applets are designed to be run from within a web (HTML) page. In this way, they differ from scripts such JavaScript which are text commands typed into the HTML page itself.
How Does It Work?
The <APPLET> tag on a web page points to a .class file in a particular location, in much the same way that an <IMG SRC=> tag points to an image. The .class file included in this applet tag is downloaded to your own computer, along with any other .class files it needs, when you arrive at a page 'containing' an applet. Once the necessary files have downloaded, your browser runs the program and places it on the web page (using the dimensions also included in the applet tag) in much the same way that an image is displayed.
The Applet Tag
The basic applet tag required to include an applet in your page looks like this:
<APPLET CODE="SomeApplet.class" WIDTH=100 HEIGHT=100> </APPLET>
This is the most basic tag possible: the three attributes CODE=, WIDTH= and HEIGHT= are all required, along with the closing </APPLET> tag.
CODE= specifies the name of the class file that the browser should fetch. Some applets consist of several class files, but only one will ever be included in the tag, and the applet's documentation will tell you which one. (Including the .class extension in this attribute is optional - your browser already knows that it's looking for a file with the .class extension, so doesn't really need telling again.)
WIDTH= and HEIGHT= specify the dimensions of the applet - the amount of real estate on a page that the browser should assign to the applet. With some applets, the dimensions will be of no more than aesthetic concern and you'll choose the size that looks best. With others, the dimensions might have to match the dimensions of an image used by the applet, or you might need to increase the size in order to display more entries in a menu applet. All CodeBase applet documentation explains how to best judge your applet dimensions.
Applet Parameters
In some cases the simple tag above is all that's needed for an applet to run.
However, some kindly applet developers include a variety of settings that the page designer can adjust to suit his own needs, by defining parameters. At CodeBase , we specialize in making our applets as user-editable as is humanly possible. Every parameter has its own name, and offers a choice of two or more settings, or values. For example, a parameter that lets you select a background color for an applet might be called BgColor. To set your chosen color, you'd include the following parameter before the final </APPLET> tag, altering the entry between the double-quotes according to the color you want to use:
<APPLET CODE="SomeApplet.class" WIDTH=100 HEIGHT=100>
<param name=BgColor value="FF0000">
</APPLET>
Note that the value you give for a parameter must be enclosed in double-quotes.
PARAM tags can be placed in any order between your main <APPLET>...</APPLET> tags. The applet is simply told to search through these PARAM tags for a parameter with a particular name, and it will do the job just as efficiently however you choose to organize them.
Many of the possible parameters you can alter will also have a default setting. If you want to use the default setting for one of the parameters, you can leave it out of your HTML code. For example, if the default background color for the applet example above were FF0000 (red), the inclusion of the parameter we've just entered is making no difference to how the applet will look on the page.
The CODEBASE Attribute
Also like an image, if you want to use an applet on your page you must make sure that the .class file referred to in the tag is available for download from the web server, and that it can be found. If the applet consists of several .class files, all must be kept together. The image tag uses a single attribute, SRC=, to specify both the name and the location of an image file (SRC="docs/images/myimage.gif", or just SRC="image.gif"). For an applet, the name and location of the class file(s) are split between two attributes, CODE= and CODEBASE=, respectively.
In the example tag given above, only the CODE= attribute is used. The browser therefore expects all necessary .class files to be in the same directory as the current HTML page. If the class files are in a different directory, such as a subdirectory called 'classes', you add the attribute CODEBASE="classes". If they're in the parent directory, use CODEBASE="../". You can also specify an absolute location, such as CODEBASE="http://www.someserver.com/somedirectory/classes".
Other Optional Attributes
Similar again to the IMG tag, the APPLET tag can take optional parameters to define alignment and spacing.
ALIGN= allows the usual values of LEFT | RIGHT | TOP | MIDDLE | BOTTOM | ABSMIDDLE | ABSBOTTOM | TEXTTOP | BASELINE. Right or Left alignment allows you to place text or an image beside an applet. Top or bottom are useful when placing multiple applets in a row (perhaps to create a menu-like strip of buttons), ensuring that the top of the applets are always aligned, regardless of how the applets differ in height.
HSPACE= and VSPACE= create additional blank space around an applet either horizontally (Hspace) or vertically (Vspace).
Java Fonts
Because Java is intended to be a cross-platform language, it can't support all the various font technologies in use on all computers (such as the TrueType fonts used by Windows). Instead, Java supports just 5 fonts which will be available on any computer that has a Java-enabled browser or the Java Development Kit installed: these are Dialog, Helvetica, Courier, TimesRoman, and the less useful Symbol. Although applets will still display text if you enter something different into their Font parameters, they will do so by using the default font. In most CodeBase applets, the default font is either Dialog or Helvetica.
More Information
For more information about Java, visit Sun Microsystems' Java site at java.sun.com.
Java is a registered trademark of Sun Microsystems, Inc.
|