Java Reference
In-Depth Information
start = last = null; // Remove the points
selectedElement = tempElement = null; // Reset elements
}
The last block of code is not entirely new - some of it has been relocated from earlier in the code for
the previous version. We have an extra condition in the original
if
expression to check for
NORMAL
mode. The next
if
tests for
MOVE
mode or
ROTATE
mode because in either case we will have changed
an element by dragging it around, and will therefore want to redraw the view. This is the one place
where we must do this explicitly because the model is not aware of these changes. If
selectedElement
is not
null
, we call
repaint()
for the view to get it redrawn and we restore
NORMAL
mode. Outside of all the
if
s we reset everything back to
null
.
If you recompile Sketcher and rerun it, you can now produce sketches like that below:
How It Works
Using a transform to position each element means that a move consists of just altering the
position
member of an element. The move operation depends on setting a
MOVE
mode for the mouse event
handling methods to respond to. A move for each element is the same: drawing the element in
XOR
mode in its original position to erase it, moving it, and then drawing it again in the new position. You
may see pixels left behind as you move elements, particularly text.
This is due to rounding in the floating-point operations mapping user coordinates to device coordinates.
They all disappear when the move is complete and the whole picture is redrawn.
Now we have made
Move
work,
Rotate
will be a piece of cake.
Rotating Elements
Clearly we are going to make use of another transform to implement this. We know how to create a
rotate | transform, so all we need to figure out is the mechanics of how the user accomplishes the
rotation of an element.