home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



Try to avoid having required parameters—if someone other than you uses your applet on their Web page, they may not know how to use all the parameters. Your applet should behave in a reasonable manner with no parameters at all.
You can make your applet more usable by implementing the getParameterInfo() method, which enables a tool such as AppletViewer to find out about the parameters you support.

Before you close the <APPLET> tag, you can include some HTML. This HTML will only be displayed if the browser did not understand the <APPLET> tag, so you should display a message telling the user what he or she is missing. The HTML in Listing 37.3 (lines 9 through 13) tells the user about the problem, puts up a graphic showing what the applet looks like, and then offers a link to the Netscape site so the end user can download Netscape Communicator.

Running Your Applet with AppletViewer

To start AppletViewer, type appletViewer HelloApplet.html at the command line. Figure 37.5 shows our applet in action. It doesn’t do much, but it works!


FIGURE 37.5  The fastest and easiest way to test an applet is with the AppletViewer.

Seeing Your Applet in a Web Browser

Eventually you’ll want your applet to appear on a Web page. Figure 37.6 shows HelloApplet viewed with Microsoft Internet Explorer. Figure 37.7 shows the same applet in Netscape Navigator.


FIGURE 37.6  Microsoft displays a gray rectangle to show the applet’s reserved space.


FIGURE 37.7  Netscape displays the applet against a transparent background.


Even an applet as simple as this one can look different when run on different Web browsers.
Depending on how widely you expect your applets to be circulated, it is usually a good idea to have several browsers (on different machines with different resolutions and different operating systems) and several different versions handy for testing.
To ensure that users are using Sun’s version of the Java Virtual Machine (rather than one written by the browser vendor), script your pages so that they load Sun’s plug-in. That way you won’t have as much variation from one machine to another.


Troubleshooting
If all doesn’t go well when you compile and load your applet, double-check your CLASSPATH variable. The most common problem new Java programmers experience is an incorrect CLASSPATH variable. If either your compiler or interpreter complain about missing classes, make sure every class archive is listed in CLASSPATH. If your class files are “loose” files in a directory, list the directory. If they’re archived into a .zip file or .jar file, you must name the archive in CLASSPATH. You shouldn’t have much trouble with HelloApplet, but it does need to find Sun’s Applet and Graphics classes.


NOTE:  If you’d like to know more about how the browser calls the various methods of your applet, look at “Life Cycle of an Applet,” later in this chapter.

Step by Step Through the Code

This section walks through the code for the HelloApplet class so you can see why each line is written the way it is. Even though only one executable statement exists (in line 8), this tiny program illustrates many of the principles you’ll use in writing any Java applet.

  The class is declared to be public.
  The class contains a member named paint().
  paint() is declared to be public and to return void.

Importing Packages

The first two lines of HelloApplet.java tell the compiler to use class definitions from two specific packages: java.applet and java.awt. (AWT stands for the Abstract Windowing Toolkit.) Specifically, these directives tell the compiler to use the Applet object from java.applet and the Graphics object from java.awt.

The import statement is simply a shorthand notation. We could have written

public class HelloApplet extends java.applet.Applet

and

public void paint (java.awt.Graphics theGraphics)

but most programmers prefer the aesthetics of import. If we planned to use many classes from java.applet or java.awt, we could have written

import java.applet.*;
import java.awt.*;

to give the compiler permission to use any class from those packages. In practice, this asterisk notation is used frequently, although we lose the immediate capability to identify where a class is defined. If we didn’t know Java well, we wouldn’t know whether the Graphics class was part of java.applet or java.awt.


NOTE:  If two classes in different packages have the same name, you can run into problems if you use the asterisk (*) notation on both packages. To make sure you get the class you want, fully qualify it in your code or in the import statement.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.