ptg7068951
Creating an Applet 239
<appletcode=”StripYahtzee.class”codebase=”javadir”height=”300”
width=”400”>
Sorry, no dice ... this requires a Java-enabled browser.
The codeattribute identifies the name of the applet’s class file. If more than
one class file is being used with an applet, codeshould refer to the class
that’s a subclass of the JAppletclass.
The codebaseapplet contains the path of the folder or subfolder where the
applet and related files can be found. If there is no codebaseattribute, all
files associated with the applet must be in the same folder as the web page
that contains the applet.In the preceding example, codebaseindicates that
the StripYahtzeeapplet can be found in the javadirsubfolder.
The heightandwidthattributes designate the size of the appletwindow
on the web page in pixels. It must be big enough to handle the things you
are displaying in your applet.
In between the opening tag, you
can provide some HTMLmarkup to display to web users whose browsers
either do not support Java or have Java turned off.
In the preceding example, the paragraph “Sorry, no dice...this requires a
Java-enabled browser” is displayed in place of the applet in browsers that
don’t run Java. You can put instructions here on how to download a Java-
enabled browser from Oracle at http://www.java.com. You also can include
hyperlinks and other HTMLelements.
Another useful attribute,align,designates how an applet is displayed in
relation to the surrounding material on the page, including text and graph-
ics. The value align=”left”lines up an applet to the left of adjacent page
elements and align=”right”to the right.
Creating an Applet
This hour’s first project is an applet that displays the string “Saluton
mondo!”, the traditional Esperanto greeting that is becoming more tradi-
tional by the hour. You get a feel for how applets are structured by re-creat-
ing the Salutonapplication from Hour 2, “Writing Your First Program,” as
a program that can run on a web page.
Create a new empty Java file called SalutonApplet, enter the text from
Listing 17.1 into the file, and save the file.