Java Reference
In-Depth Information
Expediting a move operation after clicking the Move menu item involves dragging the selected element
to its new position with button 1 down. Each move is from the previous cursor position stored in
start
to
the current cursor position when the
MOUSE_DRAGGED
event occurred. After each mouse dragged event has
been processed, the current cursor position in
last
is stored in the variable
start
, ready for the next event.
For each
MOUSE_DRAGGED
event, you move the element the distance between successive cursor positions.
TRY IT OUT: Moving Elements
Because the element classes are equipped to move, and you have equiped
SketcherView
to handle the
menu item action, you just need to add the code to the methods in
MouseHandler
. The
mousePressed()
method records the start point for a move, and it also sets up the
XOR
mode for drawing. However, you
don't need this to move or rotate existing elements, so you only want the
mousePressed()
method to set
XOR
drawing mode when the sketching mode is
NORMAL
. This is simple to fix:
public void mousePressed(MouseEvent e) {
start = e.getPoint(); // Save the
cursor position in start
buttonState = e.getButton(); // Record which
button was pressed
if(showContextMenu(e)) {
start = null;
buttonState = MouseEvent.NOBUTTON;
return;
}
if(theApp.getWindow().getElementType() == TEXT) return;