Java Reference
In-Depth Information
the mouse though, and for every occasion other than the first, you must redraw the old element so that you
effectively erase it before creating and drawing the new one. Because the graphics context is in XOR mode,
drawing the element a second time displays it in the background color, so it disappears from the view. Here's
how you can do all that:
public void mouseDragged(MouseEvent e) {
last = e.getPoint();
// Save cursor position
if(buttonState == MouseEvent.BUTTON1) {
if(tempElement == null) { // Is there an element?
tempElement = Element.createElement( // No, so create one
theApp.getWindow().getElementType(),
theApp.getWindow().getElementColor(),
start, last);
} else {
tempElement.draw(g2D);
// Yes - draw to erase it
tempElement.modify(start, last);
// Now modify it
}
tempElement.draw(g2D);
// and draw it
}
}
Directory "Sketcher 4 drawing sketch line and rectangle elements"
This method is only called when a button is pressed, and if button 1 is pressed, you are interested. You
first check for an existing element by comparing the reference in tempElement with null . If it is null , you
create an element of the current type by calling a createElement() method that you will add to the Ele-
ment class in a moment. You save a reference to the element that is created in the tempElement member of
the listener object. You pass the element type and color to the createElement() method. You obtain these
using the application object to access the application window so you can then call methods for the Sketch-
erFrame object to retrieve them. These methods don't exist in SketcherFrame yet, but it's not difficult to
implement them:
// Return the current drawing color
public Color getElementColor() {
return elementColor;
}
// Return the current element type
public int getElementType() {
return elementType;
}
Search WWH ::




Custom Search