Java Reference
In-Depth Information
Recompile Sketcher, create a few elements, and then watch them disappear before your very eyes when
you select Delete from the pop-up.
How It Works
After verifying in the
deleteElement()
method that
highlightElement
is not
null
, you call the
re-
move()
method that you added in the
SketcherModel
class way back. This will delete the element from
the list, so when the view is repainted, it is no longer displayed. The repaint occurs automatically because
the
update()
method for the view — the method that you implemented for the
Observer
interface —
is called because the model has changed. Of course, you must remember to set
highlightElement
to
null
, too; otherwise, it could get drawn by a mouse handler even though it is no longer in the model.
You have run out of easy operations. You must now deal with a not quite so easy one — the move op-
eration. To handle this you look into a new topic — transforming the user coordinate system. If you are
not of a mathematical bent, some of what I discuss here might sound complicated. But even if your math
is very rusty, you should not have too many problems. Like a lot of things, it's the unfamiliarity of the
jargon that makes it seem more difficult than it is.
TRANSFORMING THE USER COORDINATE
SYSTEM
I said when you started learning how to draw on a component that the drawing operations are specified in a
user coordinate system, and the user coordinates are converted to a device coordinate system. The conver-
sion of coordinates from the user system to the device system is taken care of by the methods in the graphics
context object that you use to do the drawing, and they do this by applying a
transformation
to the user co-
ordinates. The term
transformation
refers to the computational operations that perform the conversion.
By default, the origin, the (0,0) point in the user coordinate system, corresponds to the (0,0) point in the
device coordinate system. The axes are also coincident, too, with positive
x
heading from left to right, and
positive
y
from top to bottom. As you know, you can move the origin of the user coordinate system relative
to its default position. Such a move is called a
translation
, and this is illustrated in
Figure 20-15
.