
Go backward to Variations on a Theme
Go up to Top
Go forward to Adding an Applet to a Web Page
Tracking Mouse Movement
The skeletal applet we gave you to start with contains four additional (currently
empty) methods:
- mouseMove
- which should contain instructions telling the computer what
your applet wants it to do when the mouse is moved while its button is not
depressed. Within this method (and within mouseDrag),
the names "x" and "y" can be used to refer to
the coordinates of the pixel to which the mouse was moved.
- mouseDrag
- which determines the actions performed when the mouse is
moved with the button depressed.
- mouseExit
- which contains instructions the computer should follow when
the mouse moves out of the applet's screen region (i.e. out of the AppletViewer
window).
- mouseEnter
- which instructs the computer how to behave when the mouse
is moved back into the applet's screen region.
By adding some instructions to these methods, we can produce a more responsive
applet.
- Copy the instructions currently in "mouseDown" and then Paste them
into the body of "mouseMove".
- Edit the message included in the copy of the "drawString" command
now found within "mouseMove" so that
the computer will display a different string when following the "mouseMove"
instructions.
- Again, first try to predict how this new applet will behave. Then "Run" it
and see what actually happens.
- "Quit" to get back to the editor.
With the current version of your applet, the computer will often leave just a
fraction of your message readable with the rest lost off the edge of the window
when you move the mouse out of the window. Let's fix this by cleaning things
up when the mouse leaves the window.
- Fill in the body of the "mouseExit" method with a "clearRect" instruction
followed by a "drawString" for an appropriate (or even an inappropriate) message.
Since there is no mention of either "x" or "y" in the line specifying the name of
this method, you can't use these names in the "drawString". Instead, the
"drawString" command used here should just say what to display like the
original "drawString" found in the "mouseDown" method when you started.
- "Run" your applet to see how it works now (correct any typing errors and
"Run" again if necessary).
There are many more variations of these simple applets you could try. For those
who are inspired, we include details of some other variations of the commands
you have used and some additional commands you can include in a method at
the end of this document. We encourage you to experiment with these possibilities
on your own.
For now, however, we ask you to leave your applet as it is and move on to
learn how to get it to run under a web browser as part of a web page. So,
"Quit" both the "AppletViewer" and the Metrowerks editor leaving your
computer back at the Macintosh desktop in the Finder.
