![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
This exercise introduces delegation-based event handling. The skeleton currently sets the background color to pink (so that the applet's area within the browser window is identifiable). The objective of this exercise is to enhance the applet so that it issues a beep every time a mouse button is clicked in the applet's area of the browser window. (A mouse click is a mouse button press followed by a mouse button release, without moving the mouse.)
Skeleton Code
Tasks
The delegation-based event handling classes are in the java.awt.event package. Import the entire contents of this package.
The applet will be the event source--this functionality already exists. For a
simple task such as beeping when a mouse button is clicked, it's convenient to
design the applet to function as the event target as well. With this design, one
class ( In order to function as a target for mouse button events, the applet (class)
must implement the
With these methods, the applet can now function as an event target. Note, however, that event targets must register with event sources. (In this case, the source and the target are the same, but, nevertheless, the source must have a list of who to propagate events to, even if the "who" is itself.) Therefore, the next task is to register the applet as an event target for mouse events.
The final task is to add the functionality to issue a beep whenever there is
a mouse click. The source (the current applet) will automatically invoke the
target's (the current applet)
Where help exists, the task numbers above are linked to the step-by-step help page. Solution Source
Demonstration
This exercise only runs within browsers that support the Java Runtime Environment 1.1 and appletviewer. If you are using an appropriate browser, try this exercise. There is no output, other than the window area allocated to the applet. The background color is set to pink so that the applet area is identifiable:
Next ExerciseExercisesShort CourseCopyright 1996-2000 jGuru.com. All Rights Reserved. |