Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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();
}
}
You can also try this code with Online Java Compiler
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();
}
}
You can also try this code with Online Java Compiler
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.
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.