Java Reference
In-Depth Information
45.
public void
incrementClickCount(){
46.
clickCount++;
47.
counts.setText(Integer.toString(clickCount));
48.
}
49.
}
6.4.3
The control part
The control part has to implement the following functionality:
If the mouse is in the
MouseEventPanel
the
StatusPanel
displays the current
mouse coordinates and a 'yes'.
■
If the mouse is not in the
MouseEventPanel
, the
StatusPanel
displays the
coordinates (
■
−
1
,
−
1) and a 'no'.
When a mouse button is clicked the click count is increased by one.
■
Whenever the mouse is moved or a button is clicked, the information in the
status panel is updated.
■
The initial values to the coordinates and click count are all zero, and 'no' is
displayed.
■
To implement the first function we derive class
MyMousePositionsListener
from
MouseMotionListener
. The listener is assigned to the
MouseEventPanel
.
We override method
mouseMoved
in such a way that it sets the current coordi-
nates in the status panel. In order for the
MyMousePositionsListener
to have
access to the status panel it receives a reference to the panel in the construc-
tor. As the method
mouseMoved
is executed every time the mouse is moved (in
the panel) we always display the current coordinates. Method
mouseDragged
is
implemented with empty body because we have not specified any reaction to
dragging.
The second, third and fourth functions are realized by a mouse listener, namely
the class
MyMouseListener
derived from
MouseListener
. There we imple-
ment the methods
mouseEntered
and
mouseExited
such that they set the text
of the
inOut
label in the status panel and also the coordinates to (
1)
when exiting
MouseEventPanel
. For the fourth function, method
mouseClicked
in
MyMouseListener
is implemented to call
incrementClickCount()
of
MouseEventPanel
.Inorder for
MyMouseListener
to have access to the status
panel it receives a reference to the panel in the constructor. Methods
Mouse-
Pressed
and
MouseReleased
are not needed and therefore implemented with no
functions, i.e. with empty bodies.
This completes the application (see Figure 6.3); the listings of the two listener
classes and the driver class
MouseEventDriver
are given below.
−
1
,
−
Search WWH ::
Custom Search