Java MouseMotionListener Interface


The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. The MouseMotionListener interface is found in java.awt.event package. It has two methods.

Methods of MouseMotionListener interface

The signature of 2 methods found in MouseMotionListener interface are given below:

  1. public abstract void mouseDragged(MouseEvent e);  
  2. public abstract void mouseMoved(MouseEvent e);  

Java MouseMotionListener Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseMotionListenerExample extends Frame implements MouseMotionListener{  
  4.     MouseMotionListenerExample(){  
  5.         addMouseMotionListener(this);  
  6.           
  7.         setSize(300,300);  
  8.         setLayout(null);  
  9.         setVisible(true);  
  10.     }  
  11. public void mouseDragged(MouseEvent e) {  
  12.     Graphics g=getGraphics();  
  13.     g.setColor(Color.BLUE);  
  14.     g.fillOval(e.getX(),e.getY(),20,20);  
  15. }  
  16. public void mouseMoved(MouseEvent e) {}  
  17.   
  18. public static void main(String[] args) {  
  19.     new MouseMotionListenerExample();  
  20. }  
  21. }  

Output:






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition