MouseMotionListener Example


The events generated by the mouse come under low-level events. We have seen previously how to handle the events of a mouse when the mouse is stable with MouseListener. Let us handle the events when the mouse is in motion. Two types of actions can be generated when the mouse is in motion – just moving the mouse and while dragging (moving the mouse while mouse the key is being pressed). These two actions are handled by the two abstract methods of MouseMotionListener – mouseMoved() and mouseDragged(); both take MouseEvent as parameter.

Two programs are given, the first one being simple like the one of MouseListener and the second one is a bigger one which implements both MouseListener and MouseMotionListener.

MouseMotionListener Example changing the background of the frame as per the action.
import java.awt.*;
import java.awt.event.*;

public class MouseMotionDemo extends Frame implements MouseMotionListener
{
  public MouseMotionDemo()
  {                                                            
    addMouseMotionListener(this);                // link the frame with MML
    setSize(300,300);
    setVisible(true);
  }
                                  // override the 2 abstract methods of MML 
  public void mouseMoved(MouseEvent e)
  {
    setBackground(Color.red);
    System.out.println("Mouse is Moved");
  }
  public void mouseDragged(MouseEvent e)
  {
    setBackground(Color.blue);
    System.out.println("Mouse is Dragged");
  }
  public static void main(String args[])
  {
    new MouseMotionDemo();
  }
}


Output screen of MouseMotionListener Example

Following second program on MouseMotionListener Example implements both the mouse listeners. Where the action takes place with the mouse in the frame window, in the same spot one disc (filled oval) is displayed along with coordinates and type of action occurred.

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

public class MouseXY extends Frame implements MouseListener, MouseMotionListener
{
  int x, y;
  String str="";
  
  public MouseXY()
  {
    addMouseListener(this);
    addMouseMotionListener(this);

    setSize(300, 300);
    setVisible(true);
  } 
                                     // override ML five abstract methods
  public void mousePressed(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Pressed";
    repaint();
  }
  public void mouseReleased(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Released";
    repaint();
  }
  public void mouseClicked(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Clicked";
    repaint();
  }
  public void mouseEntered(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Entered";
    repaint();
  }
  public void mouseExited(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Exited";
    repaint();
  }
                                       // override MML two abstract methods
  public void mouseMoved(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse Moved";
    repaint();
  }
  public void mouseDragged(MouseEvent e)
  {
    x = e.getX();
    y = e.getY();
    str = "Mouse dragged";
    repaint();
  }
  public void paint(Graphics g)
  {
    g.setFont(new Font("Monospaced", Font.BOLD, 20));
    g.fillOval(x, y, 10, 10);
    g.drawString(x + "," + y,  x+10, y -10);
    g.drawString(str, x+10, y+20);
  }
  public static void main(String args[])
  {
    new MouseXY();
  }
}
MouseMotionListener Example
Output screen of MouseMotionListener Example

x = e.getX();
y = e.getY();

getX() and getY() methods of MouseEvent class return the x and y coordinates where the mouse action taken place. With fillOval() method of Graphics class a disc is drawn on the frame and with drawString() method , two strings are drawn of x and y coordinates and the type of action taken. Also learn repaint() method.

Leave a Comment

Your email address will not be published.