Java Reference
In-Depth Information
return;
}
}
// Here there is no element under the cursor so...
if(highlightElement!=null) { // If an element is highlighted
g2D = (Graphics2D)getGraphics(); // Get graphics context
highlightElement.setHighlighted(false); // ...turn off highlighting
highlightElement.draw(g2D); // Redraw the element
highlightElement = null; // No element highlighted
g2D.dispose(); // Release graphic context resources
g2D = null;
}
}
To check that highlighting works, recompile Sketcher and run it again. If you draw a few elements, you
should see them change color as the cursor moves over them.
How It Works
This method is a fair amount of code so let's work through it step by step. The first statement saves the
current cursor position in the local variable,
currentCursor
. The next two statements obtain a
Graphics2D
object and declare a variable,
element
, which we will use to store each element that we
retrieve from the model. The variable,
g2D
will be passed to the
draw()
method for any element that
we need to redraw highlighted or un-highlighted as the case may be.
We use an iterator we get from the model to go through the elements - you have seen how this works
previously. In the loop, we obtain the bounding rectangle for each element by calling its
getBounds()
method, and then call the
contains()
method for the rectangle that is returned. This will return
true
if
the rectangle encloses the point,
currentCursor
, that is passed as an argument. When we find an element
under the cursor, it is quite possible that the element is already highlighted because the element was found
last time the
mouseMoved()
method was called. This will occur when you move the cursor within the
rectangle bounding an element. In this case we don't need to do anything, so we return from the method.
If the element found is not the same as last time, we obtain a graphics context for the view since we definitely
need it to draw the new element we have found under the cursor in the highlight color. We then check that
the variable
highlightElement
is not
null
- it will be
null
if the cursor just entered the rectangle for an
element and previously none was highlighted. If
highlightElement
is not
null
we must un-highlight the
old element before we highlight the new one. To do this we call its
setHighlighted()
method with the
argument
false
, and call its
draw()
method. We don't need to involve the
paint()
method for the view
here since we are not adding or removing elements - we are simply redrawing an element that is already
displayed. To highlight the new element, we call its
setHighlighted()
method with the argument
true
,
store a reference to the element in
highlightElement
and call its
draw()
method to get it drawn in the
highlight color. We then release the graphics context resources by calling the
dispose()
method for
g2D,
set the variable back to
null
and return.
The next block of code in the method is executed if we exit the
while
loop because no element is
under the cursor. In this case we must check if there was an element highlighted last time around. If
there was, we un-highlight it, redraw it in its normal color and reset
highlightElement
to
null
.