Java Reference
In-Depth Information
At most one element can be highlighted at any given time. You need to remember which element is high-
lighted when there is one so you can reset its highlighting when it isn't under the cursor. To record a refer-
ence to the element that is under the cursor, you add a data member of type
Element
to the
SketcherView
class. When there isn't an element under the cursor, you make sure that this data member is
null
.
TRY IT OUT: Highlighting to Elements
Add the following statement after the statement that declares the
theApp
data member in the
Sketcher-
View
class definition:
private Element highlightElement;
//
Highlighted element
The
mouseMoved()
method in
MouseHandler
is going to be called very frequently, so you must make
sure it executes as quickly as possible. This means that for any given set of conditions, you execute the
minimum amount of code. Here's the implementation of the
mouseMoved()
method in the
MouseHand-
ler
class in
SketcherView
:
// Handle mouse move events
@Override
public void mouseMoved(MouseEvent e) {
Point cursor = e.getPoint();
// Get current
cursor position
for(Element element : theApp.getModel()) {
// Go through the
list...
if(element.getBounds().contains(cursor)) { // ...under the