| 9.10 |
Mouse Events - Big Java - Chapter
9 pg 421 - 429 (Cay Horstmann) |
 |
 |
If you write programs that show drawings, and you want users to
manipulate the drawings with a mouse, then you need to process mouse
events. Mouse events are more complex than button clicks or timer
ticks.
A mouse listener must implement the
MouseListener interface, which contains the following five methods:
public interface MouseListener
{
void mousePressed(MouseEvent event);
// Called when a mouse button has been pressed on a component
void mouseReleased(MouseEvent event);
// Called when a mouse button has been released on a component
void mouseClicked(MouseEvent event);
// Called when the mouse has been clicked on a component
void mouseEntered(MouseEvent event);
// Called when the mouse enters a component
void mouseExited(MouseEvent event);
// Called when the mouse exits a component
}
The
mousePressed and
mouseReleased
methods are called whenever a mouse button is pressed or released. If a
button is pressed and released in quick succession, and the mouse has
not moved, then the
mouseClicked method is called as well. The
mouseEntered and
mouseExited methods can be used to paint a user-interface component in a special way whenever the mouse is pointing inside it.
The most commonly used method is mousePressed. Users generally expect that their actions are processed as soon as the mouse button is pressed.
You add a mouse listener to a component by calling the
addMouseListener method:
public class MyMouseListener implements MouseListener
{
// Implements five methods
}
MouseListener listener = new MyMouseListener();
component.addMouseListener(listener);
In our sample program, a user clicks on a component containing a
rectangle. Whenever the mouse button is pressed, the rectangle is moved
to the mouse location. We first enhance the
RectangleComponent class and add a
moveTo method to move the rectangle to a new position.
ch09/mouse/RectangleComponent.java
1 import java.awt.Graphics;
2 import java.awt.Graphics2D;
3 import java.awt.Rectangle;
4 import javax.swing.JComponent;
5
6 /**
7 This component displays a rectangle that can be moved.
8 */
9 public class RectangleComponent extends JComponent
10 {
11 public RectangleComponent()
12 {
13 // The rectangle that the paint method draws
14 box = new Rectangle(BOX_X, BOX_Y,
15 BOX_WIDTH, BOX_HEIGHT);
16 }
17
18 public void paintComponent(Graphics g)
19 {
20 super.paintComponent(g);
21 Graphics2D g2 = (Graphics2D) g;
22
23 g2.draw(box);
24 }
25
26 /**
27 Moves the rectangle to the given location.
28 @param x the x-position of the new location
29 @param y the y-position of the new location
30 */
31 public void moveTo(int x, int y)
32 {
33 box.setLocation(x, y);
34 repaint();
35 }
36
37 private Rectangle box;
38
39 private static final int BOX_X = 100;
40 private static final int BOX_Y = 100;
41 private static final int BOX_WIDTH = 20;
42 private static final int BOX_HEIGHT = 30;
43 }
Note the call to
repaint in the
moveTo
method. As explained in the preceding section, this call causes the
component to repaint itself and show the rectangle in the new position.
Now, add a mouse listener to the component. Whenever the mouse is
pressed, the listener moves the rectangle to the mouse location.
class MousePressListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
component.moveTo(x, y);
}
// Do-nothing methods
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
It often happens that a particular listener specifies actions
only for one or two of the listener methods. Nevertheless, all five
methods of the interface must be implemented. The unused methods are
simply implemented as do-nothing methods.
 |



 |
| Copyright © 2008 John Wiley & Sons, Inc. All rights reserved. |