Running Applets
Problem
You want to run an applet.
Solution
Write some HTML and point a browser at it.
Discussion
An applet is simply a Java class that extends
java.applet.Applet, and in doing so inherits the
functionality it needs to be viewable inside a web page in a
Java-enabled web
browser.[8] All
that’s necessary is an HTML page referring to the applet. This
HTML page requires a minimum of three
attributes
, or modifiers: the applet itself, and
the width and height it needs on-screen, in screen dots or pixels.
This is not the place for me to teach you the syntax of
HTML -- there is some of that in Section 17.2 -- but I’ll show my HTML applet
template file. Many of the IDEs will write a page like
this for you if you use their “build new applet” wizards.
<HTML> <HEAD><TITLE>A Demonstration</TITLE></HEAD> <BODY> <H1>My TEMPLATE Applet</H1> <APPLET CODE="CCC.class" WIDTH="200" HEIGHT="200"> </APPLET> </BODY> </HTML>
You can probably intuit from this just about all you need to get
started. For a little more detail, see Section 17.2.
Once you’ve created this file (replacing the
CCC with the actual name of your applet) and
placed it in the same directory as the class file, you need only tell
the browser to view the HTML page, and the applet should be included
in it.
All right, so the applet appeared and it even almost worked. Make a change to the Java source and recompile. Click the browser’s Reload button. Chances are you’re still running the old version! Browsers aren’t very good ...