Java Reference
In-Depth Information
case MOVE:
// Moving an element
if(buttonState == MouseEvent.BUTTON1 && selectedElement !=
null) {
selectedElement.move(last.x-start.x, last.y-start.y); //
Move it
repaint();
start = last;
// Make start
current point
}
break;
case ROTATE:
// Rotating an element
// Execute rotate...
break;
}
}
Directory "Sketcher 10 moving and rotating elements"
Now the method only executes the previous code in NORMAL mode. For MOVE mode, if button 1 is down
and there is an element selected to move, you move it by calling its move() method and repainting the
view. The current last is start for the next MOUSE_DRAGGED event.
The final alterations to the code occur in the mouseReleased() method:
public void mouseReleased(MouseEvent e) {
if(mode == MOVE || mode == ROTATE) {
selectedElement = null;
start = last = null;
mode = NORMAL;
return;
}
// Rest of the code as before...
}
Directory "Sketcher 10 moving and rotating elements"
There is a new if condition at the beginning that checks for the sketching mode being MOVE or ROTATE .
If it is either of these, you only have to reset start , last , and selectedElement to null , and set mode
back to NORMAL . g2D is not created if the mode is not NORMAL so you don't need to reset that.
If you recompile Sketcher and rerun it, you can now produce sketches like the one shown in Figure 20-20 .
FIGURE 20-20
 
 
Search WWH ::




Custom Search