Java Programming
from the Grounds Up
Embedding Applets in your HTML Pages
A simple HTML file that loads the Variable Hello applet is shown in Figure 4. The applet specification begins with an <APPLET> tag, which has three modifiers. The code modifier specifies the name of the Java bytecode file VH.class. The width and height modifiers specify a default area which the browser should allocate in order to display the applet's graphics.
Any number of <PARAM> tags may be specified between <APPLET>'s opening and closing tags. Our example shows a single parameter with the name who and the value "all you Java programmers". The name and value modifiers are always interpreted as strings.
FIGURE 4
The Variable hello HTML file.
<title>Variable Hello</title>
<'hr>
<applet code="VH.class" width=300 height=100>
<param name=who value="all you Java programmers">
</applet>
<hr>
<a href="VH.java">The source.</a>
Since this HTML specified a value for the who parameter, the getParameter() method will place its value in the String variable who. Had there had been no <PARAM> tag defining a who parameter, then getParameter() would have returned null, and the second line of code in init() would have given the variable who the predictable default value "world". The init() next calls the method getDocumentBase() from the java.net package, and stores the result in the local variable uwhere. This variable is of type URL, and will contain a description of this page's URL.
A printable representation of the URL is obtained using the toString() method. Finally, init() constructs two greeting messages and places them in the class variables msg1 and msg2. msg1 contains the fixed string "Hello", followed by the value of who and another comma. msg2 reports the source of the greeting by combining "from" and the string form of the URL. As in Primes.java, "+" is used to join strings together.
The run() and paint() methods are both very simple. The run() method gets the default size of the applet using the preferredSize() method, and then calls resize().
This is a polite request to the browser to restore the applet's drawing area to 300x100, in case the window size has been modified in some way. run() then calls the Java method repaint(), which will arrange to completely redraw the applet's drawing area.
By a series of circuitous internal steps, this call to repaint() will ultimately invoke the paint() method, which we have overridden. paint() is called with a Graphics context g as its argument. We can now use a number of methods in the Graphics class to alter the drawing area. Here, we will simply change the text color to blue and then draw the two Strings msg1 and msg2, one below the other. Figure 5 shows the resulting output using appletviewer, the Java applet viewer from the JDK.
This example is extremely simple, but is does have all the components required of a fully functioning applet. It does not have any user interaction, however. Figure 6 shows an upgraded version of the VH applet which will respond to a user's mouse click. The HTML file that loads it is shown in Figure 6.
FIGURE 6
HTML for the Variable Interactive Hello page.
<title>Variable Interactive Hello</title>
<hr>
<applet code="VH2.class" width=300 height=300>
<param name=who value="all you Java programmers">
<param name=image value="mouse">
</applet>
<hr>
<a href="VH2.java">The source.</a>
NEXT
Mark Reynolds is a network protocol designer, Java programmer, computer animator, and fanatic mountaineer. He currently consults to Adaptive Optics Associates, a United Technologies company.
Reprinted from Web Developer® magazine, Vol. 2 No.1 Spring 1996 (c) 1996 internet.com Corporation. All rights reserved.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.