You want to run an applet.
Write a class that extends java.applet.Applet; write some HTML and point a browser at it.
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. All that's necessary is an HTML page referring to the applet. This HTML page requires an applet tag with a minimum of three attributes , or modifiers: the name of the applet itself and its onscreen width and height in screen dots or pixels. This is not the place for me to teach you HTML syntax there is some of that in Recipe 18.1 but I'll show my HTML applet template file. Many of the IDEs write a page like this if you use their "build new applet" wizards:
<html> <head><title>A Demonstration</title></head> <body> <h1>My TEMPLATE Applet</h1> <applet code="CCC" 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 Recipe 18.1. Once you've created this file (replacing the CCC with the fully qualified class name of your applet e.g., code="com.foo.MyApplet") and placed it in the same directory as the class file, you need only tell a Java-enabled web 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 at debugging applets. You can sometimes get around this by holding down the Shift key while you click Reload. But to be sure, use AppletViewer , a kind of mini-browser included in the JDK. You need to give it the HTML file, just like a regular browser. Sun's AppletViewer (shown in Figure 1-9 under Windows) has an explicit Reload button that actually reloads the applet. And it has other features, such as debugging hooks, and other information displays. It also has a View Tag option that lets you resize the window until the applet looks best, and then you can copy and paste the tag including the adjusted width and height attributes into a longer HTML document.
Figure 1-9. Sun JDK AppletViewer
The Mac OS X runtime includes both the standard AppletViewer from Sun and Apple's own implementation (available as /Applications/Utilities/Java/Applet Launcher, shown in Figure 1-10), which is more colorful but slightly different. It has no Reload item in its menu; you close the Applet's window and press the Launch button to reload. It also lets you load a new HTML file by typing in the URL field (or pressing Open... and browsing), which is more efficient than closing and restarting the traditional AppletViewer when the HTML file changes or when you want to invoke a different file.
Figure 1-10. Apple Mac OS X applet launcher
Neither the Sun version nor the Apple version is a full applet runtime; features such as jumping to a new document do not work. But they are very good tools for debugging applets. Learn to use the AppletViewer that comes with your JDK or IDE.
The bad news about applets is that they either can't use features of current Java versions or they run into the dreaded browser-incompatibility issue. In Recipe 23.6, I show how to use the Java Plug-in to get around this. In Recipe 23.13, I talk about Java Web Start, a relatively new technique for distributing applications over the Web in a way similar to how applets are downloaded; with JWS, programs are downloaded using HTTP but stored as, and run as, regular applications on your system's local disk.