Java Programming
from the Grounds Up
Adding Interaction
This Variable Interactive Hello applet has init(), run() and paint() methods, as before. It also has a mouseDown() method, which overrides the built-in method of that name. The init() method for this VH2 class is almost the same as the init() method in VH, except that it also looks for a parameter named image. If it is found, then the class variable imgnam will hold its value.
In this case, the init() method also attempts to access that image using the getImage() method. The first argument to that method is the URL of the current page, stored in uwhere; the second specifies a relative pathname for the image. Since our HTML gives the value of imgnam as "mouse," a file named mouse.gif in the subdirectory image will be retrieved, if possible.
FIGURE 5
The interactive version of the Variable Hello applet.
1: /**
2: A simple Java applet to issue an HTML-specified greeting,
3: with a tiny bit of user interaction
4: @author Mark C. Reynolds
5: @version 1.0
6: */
7:
8: import java.awt.*;
9: import java.net.*;
10:
11: public class VH2
extends java.applet.Applet implements Runnable {
12:
13: String msg1;
14: String msg2;
15: String imgnam = null;
16: Image img;
17: boolean doimg = false;
18:
19: public void init() {
20: String who;
21: String where;
22: URL uwhere;
23:
24: who = getParameter("who");
25: if ( who == null ) who = "world";
26: uwhere = getDocumentBase();
27: where = uwhere.toString();
28: imgnam = getParameter("image");
29: if ( imgnam != null ) {
30: img = getImage(uwhere, "image/" + imgnam + ".gif");
31: }
32: msg1 = "Hello, " + who + ",";
33: msg2 = "from " + where;
34: }
35:
36: public boolean mouseDown(Event e, int x, int y) {
37: doimg = true;
38: repaint();
39: return(true);
40: }
41:
42: public void paint(Graphics g) {
43: Dimension d = size();
44:
45: g.drawRect(0, 0, d.width - 1, d.height - 1);
46: g.setColor(Color.blue);
47: g.drawString(msg1, 10, 15);
48: g.drawString(msg2, 10, 30);
49: g.drawString("Click anywhere to see
mouse event tracking", 10, 45);
50: if ( doimg == true && imgnam != null ) {
51: g.drawImage(img, 10, 60, this);
52: g.setColor(Color.red);
53: g.drawString("Squeek!", 10, 70 + img.getHeight(this));
54: }
55: }
56:
57: public void run() {
58: resize(preferredSize());
59: repaint();
60: }
61: }
The mouseDown() method is called whenever a mouse button has been pressed. This method sets the class variable doimg to true, and then calls repaint to redraw the applet. Note that doimg is initialized to false, and is only toggled to true in the event of a user mouse click. The method returns true to indicate that it has handled the event.
The paint() method used here is significantly different than in VH. It begins by determining the current size of the Applet's drawing area via a call to size(). It then uses the drawRect method of the Graphics class to draw a blank rectangle covering the entire drawing area (a quick-and-dirty way to clear it). As in VH, the applet then sets the text color to blue and prints the two greeting strings, followed by an invitation to the user to click anywhere.
If the class variable doimg is true and the class variable imgnam is not null, then the image retrieved during the init() method will be displayed, followed by the message "Squeek!" in red. The method call img.getHeight(this) returns the height of the image. This is used in the calculation of the y coordinate of the "Squeek!" string, so that it will appear just below the mouse GIF.
Logically, how does this work? When the page is first loaded, init() is called, followed by run(). The run() method calls repaint(), which generates its own call to paint(). At this point, doimg will still have its initial value of false. Therefore, only the two greeting strings will be drawn, followed by the enticing invitation to click anywhere. The applet will then sit in an event loop, waiting for something to happen.
If the user accepts the invitation and clicks within the applet's drawing area, then the mouseDown() method will be called by AWT's event handler. Our mouseDown() method will then set doimg to true and call repaint(). This forces a second pass through the paint() method. This time the three strings will again be drawn, but the image and the final red "Squeek!" will be displayed as well. The user has caused a GIF and a string to appear by clicking within the applet.
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.