Java Reference
In-Depth Information
implementing an interface means we must provide definitions for all methods in
the interface. Therefore we provide empty methods corresponding to the other
events. When those events are generated, the empty methods are called, but no
code is executed. In Chapter 9 we discuss a technique for creating listeners that
lets us avoid creating such empty methods.
Let's look at an example that responds to two mouse-oriented events. The
RubberLines program shown in Listing 8.19 draws a line between two points.
The first point is determined by the location at which the mouse is first pressed
down. The second point changes as the mouse is dragged while the mouse but-
ton is held down. When the button is released, the line remains fixed between
the first and second points. When the mouse button is pressed again, a new line
is started.
The panel on which the lines are drawn is represented by the RubberLinesPanel
class shown in Listing 8.20. Because we need to listen for both a mouse pressed
event and a mouse dragged event, we need a listener that responds to both mouse
events and mouse motion events. Note that the listener class in this example
implements both the MouseListener and MouseMotionListener interfaces. It
must therefore implement all methods of both interfaces. The two methods of
interest, mousePressed and mouseDragged , are implemented to accomplish our
goals, and the other methods are given empty definitions to satisfy the interface
contract.
When the mousePressed method is called, the variable
point1 is set. Then, as the mouse is dragged, the vari-
able point2 is continually reset and the panel repainted.
Therefore, the line is constantly being redrawn as the mouse
is dragged, giving the appearance that one line is being
stretched between a fixed point and a moving point. This
effect is called rubberbanding and is common in graphical programs.
Note that, in the RubberLinesPanel constructor, the listener object is added
to the panel twice: once as a mouse listener and once as a mouse motion listener.
The method called to add the listener must correspond to the object passed as the
parameter. In this case, we had one object that served as a listener for both cat-
egories of events. We could have had two listener classes if desired: one listening
for mouse events and one listening for mouse motion events. A component can
have multiple listeners for various event categories.
Also note that this program draws one line at a time. That is, when the user
begins to draw another line with a new mouse click, the previous one disappears.
This is because the paintComponent method redraws its background, eliminating
the line every time. To see the previous lines, we'd have to keep track of them,
perhaps using an ArrayList as was done in the Dots program. This modification
to the RubberLines program is left as a programming project.
KEY CONCEPT
Rubberbanding is the graphical
effect caused when a shape seems to
expand as the mouse is dragged.
VideoNote
Example using
rubberbanding
and arrays.
 
Search WWH ::




Custom Search