Java Reference
In-Depth Information
a line, the point where you depress the mouse button is the start point for the line, and the point where you
release the button is the end point. This process is illustrated in Figure 19-23 .
FIGURE 19-23
As the user drags the mouse with the button down, Sketcher displays the line as it looks at that point.
Thus, the line is displayed dynamically all the time the mouse cursor is being dragged with the left button
pressed. This process is called rubber-banding .
You can use essentially the same process of pressing the mouse button and dragging the cursor for all four
of the geometric shapes you saw when I discussed the Element class. Two points define a line, a rectangle,
or a circle — the cursor position where the mouse button is pressed and the cursor position where the mouse
button is released. For a line the two points are the end points, for a rectangle they are opposite corners, and
for a circle they are the center and a point on the circumference. This implies that the constructors for these
have three parameters, corresponding to the two points and the color. A curve is a little more complicated in
that many more than two points are involved, so I'm deferring discussion of that until later.
All the operations that deal with the mouse in your program involve handling events that the mouse gen-
erates. Let's look at how you handle mouse events to make drawing lines, rectangles, and circles work.
Handling Mouse Events
Because all the drawing operations for a sketch are accomplished using the mouse, you must implement the
process for creating elements within the methods that handle the mouse events. The mouse events you're
interested in originate in the SketcherView object because the mouse events that relate to drawing shapes
originate in the content pane for the application window, which is the view object. You make the view re-
sponsible for handling all its own events, which includes events that occur in the drawing process as well as
interactions with existing shapes.
Drawing a shape, such as a line, interactively involves you in handling three different kinds of mouse
event. Table 19-1 is a summary of what they are and what you need to do in Sketcher when they occur:
TABLE 19-1 : Mouse Events for Drawing Shapes
 
 
 
 
Search WWH ::




Custom Search