Java Reference
In-Depth Information
}
private Rectangle2D.Double rectangle;
private final static long serialVersionUID = 1001L;
}
// Abstract methods & fields in Element class...
}
Directory "Sketcher 4 drawing sketch line and rectangle elements"
If you comment out the lines in the createElement() method that creates circles and curves you should
be able to recompile the Sketcher program and draw rectangles as well as lines — in various colors, too.
A typical high-quality artistic sketch that you are now able to create is shown in Figure 19-25 .
FIGURE 19-25
How It Works
The code that enables lines and rectangles to be drawn work in essentially the same way. You can drag
the mouse in any direction to create a rectangle or a line. Both types rubber-band as you drag the mouse
with button 1 down.
The type of element that is created is determined as a line by default, but you can select Rectangle from
the Element menu. When you are creating a rectangle, the constructor sorts out the correct coordinates
for the top-left corner. This is necessary because the rectangle is being defined from any of its diagonals,
because the rectangle is always displayed from the point where the mouse button was pressed to the cur-
rent cursor position. The point where the mouse button is pressed can be at any of the four corners of the
rectangle. Because the top-left corner always defines the position of a Rectangle2D object, you have to
work out where that is.
The getBounds() method in the Element.Rectangle class calls the getBoundingRectangle() method
inherited from the base class, Element . You can see why it is necessary to increase the width and height
 
 
Search WWH ::




Custom Search