Click Here!
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


Compiling the Applet

Open a command prompt window. Make sure you’re in your development directory and then type javac HelloApplet.java. Remember that the filename must match the name of the class exactly—including capitalization. Even on a Windows system, which typically is case-insensitive, you must type the filename the way the compiler expects to see it. Four outcomes are possible whenever you run javac:

  The command interpreter cannot find javac—Check your PATH variable to make sure the bin subdirectory of your JDK folder is in the path.
  The compiler returns without comment—Your code has compiled successfully.
  Your compiler emits one or more warnings—You should examine the warning to see if you’ve made a coding error.
  Your compiler emits one or more errors—You have made a coding error.

You must eliminate the causes for all compiler errors before you can run the applet. You should strive to eliminate all warnings as well. Although your program may run, warnings are an indication that you may have made a mistake. You should almost always be able to rewrite the code in such a way as to eliminate the warnings.

Occasionally you may get a warning that tells you that you are using a deprecated method. Rerun the compiler with the -deprecated switch to find out the exact problem. Deprecated methods are those that are still supported but are no longer recommended—they may be removed completely in some future release. When writing new code, you should eliminate all deprecated calls.

After your code has compiled successfully, it’s time to load your applet into a Web page.

• To learn more about the Graphics class, see “Displaying Graphics” on p. 1078.

Running the Applet

If we were writing an application, we could invoke the Java interpreter from the command line to run the class. An applet, however, requires a browser environment, so we’ll have to write some HTML to display the applet.

Writing the HTML

Listing 37.2 shows a simple Web page that includes the HelloApplet applet. This Web page will work with the appletviewer—you’ll want to improve the HTML by adding a head, title, background color, and perhaps some text before you use it on your Web site.

Listing 37.2 helloApplet.html—You Must Write an HTML File to Test an Applet


<HTML>
 <BODY>
 <APPLET CODE=”HelloApplet.class” WIDTH=”200" HEIGHT=”200">
 </APPLET>
 </BODY>
 </HTML>


Your HTML source file is not subject to the same strict rules about long filenames and case sensitivity as your Java source files. The file here is called helloApplet.html. You can pretty much name yours anything you want, provided it has an .htm or .html suffix.

ON THE WEB
http://java.sun.com/products/plugin/ You can ensure that the user is running your applet in the latest version of Java by configuring your HTML pages to use the Sun Java plug-in. See http://java.sun.com/products/plugin/currentVersion/docs/tags.html#Any to learn how to convert <APPLET> tags to JavaScript that will load the plug-in correctly on either a Microsoft or a Netscape browser. (In this URL, replace currentVersion with the current version number, such as 1.1.1 or 1.2.) You can convert your pages automatically by using Sun’s Java Plug-In HTML Converter, described at http://java.sun.com/products/plugin/features.html.

Getting Started Quickly with a Simple <Applet> Tag The <APPLET> tag shown in Listing 37.2 is about as simple as an <APPLET> tag can get. You must include the CODE attribute to tell the browser which class to load. You need to specify the height and width of the graphical space so the browser can allocate it. Other than that, everything in this tag is defaulted.

Note that you must close the applet tag with </APPLET>. Many new Java users forget the closing tag, leaving the appletviewer confused.

Using the Full <Applet> Tag From time to time, you may need to add other elements to the <APPLET> tag. Listing 37.3 shows a more complete example.

Listing 37.3 bigApplet.html—You Can Use More Attributes, Parameters, and Even HTML in the <APPLET> Tag


 <HTML>
 <BODY>
 <APPLET CODEBASE=”http://myserver.mydomain.com/applets”>
 CODE=”SomeApplet.class” WIDTH=”200" HEIGHT=”200"
 ALT=”A simple applet” NAME=”hello”
 ALIGN=”Center” VSPACE=”2" HSPACE=”2">
 <PARAM NAME=”Auto” VALUE=”True”>
 <PARAM NAME=”Interface” VALUE=”Full”>
 Your browser doesn’t understand Java. If you had a Java-enabled
⇒browser you’d see something like this:<BR>
 <IMG SRC=”applet.gif” ALT=”Image of Applet” HEIGHT=”200" WIDTH=
⇒”200">
 You can get a Java-aware browser from
 <A HREF=”http://home.netscape.com/”>Netscape Communications</A>.
 </APPLET>
 </BODY>
 </HTML>

In this version of the <APPLET> tag, we’ve specified a CODEBASE where the browser should look for the class file. (By default the browser asks for the applet from the same server and directory that provided the HTML page.) This version also includes some descriptive text about the applet (in ALT) and a NAME (for use by other applets or by JavaScript).

• For an example of JavaScript that communicates with an applet, see “JavaScript to Java Communication” on p. 565.

The final set of attributes, ALIGN, VSPACE, and HSPACE, provides alignment and vertical and horizontal spacing.

Following the opening <APPLET> tag, you may place parameters (in <PARAM> tags). Give each parameter a name and a value; the applet will be able to read the parameters.

It’s a good idea to make your applets customizable with parameters. Parameterized applets are more useful as components.


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.