Java MouseMotionListener Interface

The MouseMotionListener interface in Java is part of the java.awt.event package and is used to handle mouse motion events in a GUI application. It provides two methods:

  1. public void mouseDragged(MouseEvent e) – This method is called when the mouse is moved with one or more of its buttons pressed down.
  2. public void mouseMoved(MouseEvent e) – This method is called when the mouse is moved without any of its buttons pressed down.

Both of these methods take a MouseEvent object as a parameter, which provides information about the mouse event such as the mouse’s location and which button was pressed.

To use the MouseMotionListener interface, you need to implement it in a class and add it to the component that you want to monitor for mouse motion events using the addMouseMotionListener() method. When a mouse motion event occurs on the component, the appropriate method in the MouseMotionListener will be called.

Methods of MouseMotionListener interface:

The MouseMotionListener interface in Java has two methods:

  1. public void mouseDragged(MouseEvent e) – This method is called when the mouse is moved with one or more of its buttons pressed down. It is used to handle mouse drag events.

The MouseEvent object passed to this method contains information about the mouse event such as the mouse’s location, the button(s) being pressed, and the number of clicks.

  1. public void mouseMoved(MouseEvent e) – This method is called when the mouse is moved without any of its buttons pressed down. It is used to handle mouse move events.

The MouseEvent object passed to this method contains information about the mouse event such as the mouse’s location.

Both of these methods are called asynchronously by the event dispatch thread. If your implementation of either method takes a long time to execute, it can cause the GUI to become unresponsive. Therefore, it is important to keep the implementation of these methods as short and efficient as possible.

To use the MouseMotionListener interface, you need to implement it in a class and add it to the component that you want to monitor for mouse motion events using the addMouseMotionListener() method. When a mouse motion event occurs on the component, the appropriate method in the MouseMotionListener will be called.

Java MouseMotionListener Example:

Here’s an example of using the MouseMotionListener interface in Java:

import java.awt.*;
import java.awt.event.*;

public class MyPanel extends Panel implements MouseMotionListener {
    private int mouseX, mouseY;
    
    public MyPanel() {
        addMouseMotionListener(this);
    }
    
    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
        repaint();
    }
    
    public void mouseDragged(MouseEvent e) {
        // do nothing
    }
    
    public void paint(Graphics g) {
        g.drawString("Mouse coordinates: " + mouseX + ", " + mouseY, 20, 20);
    }
    
    public static void main(String[] args) {
        Frame frame = new Frame("MouseMotionListener Example");
        MyPanel panel = new MyPanel();
        frame.add(panel);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

In this example, we create a custom panel called MyPanel that implements the MouseMotionListener interface. We add the panel as a mouse motion listener to itself in the constructor using addMouseMotionListener(this).

When the mouse is moved over the panel, the mouseMoved() method is called and updates the mouseX and mouseY variables with the current mouse coordinates. The repaint() method is then called to redraw the panel and display the new coordinates.

In the paint() method, we use the Graphics object to draw the mouse coordinates on the panel.

In the main() method, we create a frame and add the MyPanel instance to it. We set the size of the frame to 400×400 pixels and make it visible.