June 7, 2011, 1:44 a.m.
posted by pythonrocks
How to Write a Mouse-Motion ListenerMouse-motion events tell you when the user uses the mouse (or a similar input device) to move the onscreen cursor. For information on listening for other kinds of mouse events, such as clicks, see How to Write a Mouse Listener (page 689). For information on listening for mouse wheel events, see How to Write a Mouse Wheel Listener (page 699). If your program needs to detect both mouse events and mouse-motion events, you can use Swing's convenient MouseInputAdapter class, which implements both MouseListener and Mouse-MotionListener. Figure shows an example with a mouse-motion listener. It's exactly like the example in How to Write a Mouse Listener (page 689), except for substituting MouseMotionListener[28] for MouseListener, implementing the mouseDragged and mouseMoved methods instead of the mouse listener methods, and displaying coordinates instead of numbers of clicks.
Figure. The MouseMotionEventDemo application.
Try This:
Here's the code from MouseMotionEventDemo.java that implements the mouse-motion event handling:
public class MouseMotionEventDemo extends JPanel
implements MouseMotionListener {
//...in initialization code:
//Register for mouse events on blankArea and panel.
blankArea.addMouseMotionListener(this);
addMouseMotionListener(this);
...
}
public void mouseMoved(MouseEvent e) {
saySomething("Mouse moved", e);
}
public void mouseDragged(MouseEvent e) {
saySomething("Mouse dragged", e);
}
void saySomething(String eventDescription, MouseEvent e) {
textArea.append(eventDescription
+ " (" + e.getX() + "," + e.getY() + ")"
+ " detected on "
+ e.getComponent().getClass().getName()
+ newline);
}
}
A more interesting example is SelectionDemo, which is discussed in Introduction to Painting Concepts (page 134) in Chapter 6. The program draws a rectangle illustrating the user's current dragging. To do this, it must implement an event handler for three kinds of mouse events: mouse presses, mouse drags, and mouse releases. To be informed of all these events, the handler must implement both the MouseListener and MouseMotionListener interfaces, and be registered as both a mouse listener and a mouse-motion listener. To avoid having to define empty methods, the handler doesn't implement either listener interface directly. Instead, it extends MouseInputAdapter, as the following code snippet shows. ...//where initialization occurs: MyListener myListener = new MyListener(); addMouseListener(myListener); addMouseMotionListener(myListener); ... private class MyListener extends MouseInputAdapter { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); currentRect = new Rectangle(x, y, 0, 0); updateDrawableRect(getWidth(), getHeight()); repaint(); } public void mouseDragged(MouseEvent e) { updateSize(e); } public void mouseReleased(MouseEvent e) { updateSize(e); } ... void updateSize(MouseEvent e) { int x = e.getX(); int y = e.getY(); ... repaint(...); } The Mouse-Motion Listener APIFigure lists the methods in the MouseMotionListener. Each mouse-motion event method has a single parameter—and it's not called MouseMotionEvent. Instead, each mouse-motion event method uses a MouseEvent argument. You can find methods defined by the Mouse-Event class and its superclass InputEvent in Figure (page 692) and Figure (page 693), respectively. Also refer to the relevant API documentation at:
Examples That Use Mouse-Motion ListenersThe following examples use mouse-motion listeners.
|
- Comment
