|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Compiling the AppletOpen a command prompt window. Make sure youre in your development directory and then type javac HelloApplet.java. Remember that the filename must match the name of the class exactlyincluding 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:
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 recommendedthey may be removed completely in some future release. When writing new code, you should eliminate all deprecated calls. After your code has compiled successfully, its time to load your applet into a Web page. To learn more about the Graphics class, see Displaying Graphics on p. 1078. Running the AppletIf 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 well have to write some HTML to display the applet. Writing the HTMLListing 37.2 shows a simple Web page that includes the HelloApplet applet. This Web page will work with the appletvieweryoull 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.htmlYou Must Write an HTML File to Test an Applet <HTML> <BODY> <APPLET CODE=HelloApplet.class WIDTH=200" HEIGHT=200"> </APPLET> </BODY> </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.htmlYou 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 doesnt understand Java. If you had a Java-enabled ⇒browser youd 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, weve 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. Its a good idea to make your applets customizable with parameters. Parameterized applets are more useful as components.
|
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. |