Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
MouseMotionListener
3.
Methods of MouseMotionListener
4.
Examples
4.1.
MouseMotionListener Example 1
4.2.
MouseMotionListener Example 2
5.
Frequently Asked Questions
5.1.
What is MouseMotionListener?
5.2.
What is the use of MouseMotionListener?
5.3.
What is the difference between MouseMotionListener and Java MouseListener?
5.4.
Which command does not generate any events?
5.5.
What are the methods present in MouseMotionListener?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

MouseMotionListener

Author yuvatimankar
1 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

We all have seen interactive applications that perform some action whenever the mouse is in motion. But how do internal things work? How to implement them in Java? To answer all these questions, we have MouseMotionListener. MouseMotionListener is an interface in the java.awt.package, and it is helpful in interacting with the user whenever the mouse is in motion.

In this article, we will learn about MouseMotionListener, its declaration, and its methods with detailed examples. So let’s get started with our article.

Also Read About, Multithreading in java and Hashcode Method in Java

MouseMotionListener

MouseMotionListener is an interface in java.awt.event package. MouseMotionListener operates the events when the mouse is in motion. It is alerted whenever we move or drag the mouse. MouseMotionListener consists of two methods. We will discuss them in detail in the upcoming sections.

Read More About, Basics of Java

The syntax for declaring MouseMotionListener is as follows:

public interface MouseMotionListener
extends EventListener
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods of MouseMotionListener

Whenever a mouse is moved or dragged, an object must be present in the program, which helps in implementing the interface. An event will be generated on dragging the mouse or moving the mouse; for this, we will use some methods. MouseMotionListener consists of two methods, which are as follows:

void mouseDragged(MouseEvent e) It is invoked whenever the mouse is clicked and then dragged.
void mouseMoved(MouseEvent e) It is invoked whenever the mouse cursor has been moved but no buttons are pressed.

Examples

Let us see some examples of MouseMotionListener.

MouseMotionListener Example 1

In this example, we will see how the java MouseMotion is worked whenever we drag or move the mouse on the screen. In this example, we will create a class called NInja which will handle all the functionalities of the program, and then we will style some of the components.

import java.awt.*;  
import java.awt.event.*;  
public class MouseMotionListenerExample extends Frame implements MouseMotionListener{  
    MouseMotionListenerExample(){  
        addMouseMotionListener(this);  
       //setting size
        setSize(300,300);  
        setLayout(null);  
        setVisible(true);  
    }  
    
// Using method
public void mouseDragged(MouseEvent ev) {  
    Graphics g=getGraphics();  
// Setting color
    g.setColor(Color.BLUE);  
    g.fillOval(ev.getX(),ev.getY(),20,20);  
}  

// Method 
public void mouseMoved(MouseEvent e) {}  

// Main method
public static void main(String[] args) {  
    new MouseMotionListenerExample();  
}    
}

 

Output:

MouseMotionListener Output

As you can see, when we dragged the mouse, MouseMotionListener was identified, giving us the result as shown above.

Try and compile it by yourself on java online compiler.

MouseMotionListener Example 2

In this example, we will try to find the coordinates wherever the mouse is dragged or moved.In this example, we have created a class known as Paint, which handles all the program functions. And there is method known as mouseDragged which is used to identify the coordinates.

import java.awt.*;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseMotionListener;  

// Creating class
public class Paint extends Frame implements MouseMotionListener{  
    Label l;  
    Color c=Color.BLUE;  
    Paint(){  
    l=new Label();  
    l.setBounds(20,40,100,20);  
    add(l);  
      
    addMouseMotionListener(this);  
      
    setSize(400,400);  
    setLayout(null);  
    setVisible(true);  
}  

// Using method
public void mouseDragged(MouseEvent ev) {  
// Getting cordinates
    l.setText("X="+ev.getX()+", Y="+ev.getY());  
    Graphics g=getGraphics();  
    
// Setting color
    g.setColor(Color.RED);  
    g.fillOval(ev.getX(),ev.getY(),20,20);  
}  

// Using method
public void mouseMoved(MouseEvent e) {  
//method for identifying cordinate
    l.setText("X="+ev.getX()+", Y="+ev.getY());  
}  

// Main method
public static void main(String[] args) {  
    new Paint();  
}  
}  

 

Output:

MouseMotionListener Output

As you can see, with the help of mouseDragged method, we are able to find the coordinates of the mouse whenever its is dragged or moved, as shown in the above output.

Try and compile it by yourself on java online compiler.

Frequently Asked Questions

What is MouseMotionListener?

MouseMotionListener is an interface notified whenever you move or drag the mouse in the output frame.

What is the use of MouseMotionListener?

MouseMotionListener interface is used for obtaining mouse motion events on a component.

What is the difference between MouseMotionListener and Java MouseListener?

Java MotionListener Interface is used when the mouse is in stable mode, whereas the Java MouseListener interface is used when the mouse is in motion while handling the mouse event.

Which command does not generate any events?

The command known as TextArea objects does not generate any events. 

What are the methods present in MouseMotionListener?

MouseMotionListener consists of two methods: void moseDragged and void mouseMoved.

Conclusion

This article extensively discussed MouseMotionListener and its different methods. We started with an introduction of MouseMotionListener, then saw some of the methods of MouseMotionListener, and after that, we have seen some examples of handling MouseMotionListener. 

After reading about MouseMotionListener, are you not feeling excited to read/explore more articles on the topic of Java? Don’t worry; Coding Ninjas has covered you. To learn, see Introduction to JavaBasics of JavaLearn getting started with Java, and Learn OOPs in Java; also, you can enroll in Basics of Java with Data structure and algorithms. Also, you can visit our website, code studio!

You can refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, JavascriptSystem Design, and many more; also, if you want to test your competency in coding; you can check out the mock test series and participate in the contests hosted on Coding Ninjas Studio however if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Uber, Microsoft, etc. you must look at the problemsinterview experiences, and interview bundle for placement preparations.

However, you can consider our paid courses to give your career an edge among others.

Do upvote our blogs if you find them engaging and helpful!

Happy learning!

Previous article
Java ActionListener
Next article
Java KeyListener
Live masterclass