Java Reference
In-Depth Information
tempElement = createElement(start, last); // No, so create one
} else {
tempElement.draw(g2D); // Yes - draw to erase it
tempElement.modify(start, last); // Modify it
}
tempElement.draw(g2D); // and draw it
} else if(button1Down && mode == MOVE && selectedElement != null) {
selectedElement.draw(g2D); // Draw to erase the element
selectedElement.move(last.x-start.x, last.y-start.y); // Move it
selectedElement.draw(g2D); // Draw in its new position
start = last; // Make start current point
}
}
Now we only execute the previous code in
NORMAL
mode. For
MOVE
mode, if button 1 is down and
there is an element selected to move, we move it by erasing it at the current position, moving the
element by calling its
move()
method, and drawing it at the new position. The current
last
will be
start
for the next
MOUSE
_
DRAGGED
event.
The final alterations to our code occur in the
mouseReleased()
method:
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()) {
start = e.getPoint();
if(highlightElement==null)
theApp.getWindow().getPopup().show((Component)e.getSource(),
start.x, start.y);
else
elementPopup.show((Component)e.getSource(), start.x, start.y);
} else if((e.getButton()==MouseEvent.BUTTON1) &&
(theApp.getWindow().getElementType() != TEXT) && mode == NORMAL) {
button1Down = false; // Reset the button 1 flag
if(tempElement != null) {
theApp.getModel().add(tempElement); // Add element to the model
tempElement = null; // No temporary element now stored
}
} else if((e.getButton()==MouseEvent.BUTTON1) &&
(mode == MOVE || mode == ROTATE)) {
button1Down = false; // Reset the button 1 flag
if(selectedElement != null)
repaint();
mode = NORMAL;
}
if(g2D != null) {
g2D.dispose(); // Release graphic context resource
g2D = null; // Set it to null
}