Java Reference
In-Depth Information
Add the following declaration to
SketchView
:
private int mode = NORMAL;
We will add the definitions of these operating modes to the
Constants
interface by adding the
following statements:
// Operating modes
int NORMAL = 0;
int MOVE = 1;
int ROTATE = 2;
When we set the operating mode to other than
NORMAL
, the methods dealing with mouse events will
need to know to which element the mode applies, so we will add another member to
SketchView
to
record this:
private Element selectedElement;
Now we can implement
actionPerformed()
in the
SketchView
class as:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == moveItem) {
mode = MOVE;
selectedElement = highlightElement;
} else if(source == deleteItem) {
if(highlightElement != null) { // If there's an element
theApp.getModel().remove(highlightElement); // then remove it
highlightElement = null; // Remove the reference
}
} else if(source == rotateItem) {
mode = ROTATE;
selectedElement = highlightElement;
} else if(source == sendToBackItem) {
if(highlightElement != null) {
theApp.getModel().remove(highlightElement);
theApp.getModel().add(highlightElement);
highlightElement.setHighlighted(false);
highlightElement = null;
...
All the moving of the highlighted element will be managed in the
mouseDragged()
method in the
MouseHandler
inner class to
SketchView
.