Using Components, the GUI Building Blocks |
Mouse motion events tell you when the user uses the mouse (or a similar input device) to move the onscreen cursor. For information on tracking other mouse event types such as clicks, see How to Write a Mouse Listener.Mouse Motion Event Methods
TheMouseMotionListener
interface and its corresponding adapter class,MouseMotionAdapter
, contain two methods:
void mouseDragged(MouseEvent)
- Called by the AWT in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the preceding mouse-pressed event, even if the cursor is no longer over that component.
void mouseMoved(MouseEvent)
- Called by the AWT in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.
Examples of Handling Mouse Motion Events
The following applet contains a mouse-motion listener. It's exactly like the applet in How to Write a Mouse Listener, except for substitutingMouseMotionListener
forMouseListener
, and implementing themouseDragged
andmouseMoved
methods instead of the mouse-listener methods. You can find the applet's code inMouseMotionEventDemo.java
andBlankArea.java
.
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1 browser, such as HotJava, the JDK Applet Viewer (appletviewer
), or certain versions of Netscape Navigator and Internet Explorer. For more information about running applets, refer to About Our Examples.
Try this:
- Move the cursor into the yellow rectangle at the top of the applet.
You'll see one or more mouse-moved events.- Press and hold the mouse button, and then move the mouse so that the cursor is outside the yellow rectangle.
You'll see mouse-dragged events.
The following code is from an event handling class in the
RectangleDemo.java
source file. This class handles three kinds of events: mouse presses, mouse drags, and mouse releases. These events correspond to themousePressed
method (fromMouseListener
),mouseDragged
(fromMouseMotionListener
),mouseReleased
(fromMouseListener
). Thus, this class must implement bothMouseListener
andMouseMotionListener
. To avoid having to define too many empty methods, this class doesn't implementMouseListener
directly. Instead, it extends MouseAdapter and implements onlyMouseMotionListener
directly....//where initialization occurs: MyListener myListener = new MyListener(); addMouseListener(myListener); addMouseMotionListener(myListener); ... class MyListener extends MouseAdapter implements MouseMotionListener { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); currentRect = new Rectangle(x, y, 0, 0); repaint(); } public void mouseDragged(MouseEvent e) { updateSize(e); } public void mouseMoved(MouseEvent e) { //Do nothing. } public void mouseReleased(MouseEvent e) { updateSize(e); } void updateSize(MouseEvent e) { int x = e.getX(); int y = e.getY(); currentRect.setSize(x - currentRect.x, y - currentRect.y); repaint(); } }
Event Methods Used by Mouse-Motion Listeners
Each mouse motion event method has a single parameter -- and it's not calledMouseMotionEvent
! Instead, mouse motion event methods useMouseEvent
objects. See Writing a Mouse Listener for information about theMouseEvent
class.
Using Components, the GUI Building Blocks |