If we want to obtain a user input through a mouse, we need to create an object of a listener class that implements
MouseListener interface or MouseMotionListener interface.
To define what should happen for each mouse event, a method of MouseLister or MouseMotionListener should be defined. Methods in MouseListener interface: void mousePressed(MouseEvent event)– a mouse button is pressed down. void mouseReleased(MouseEvent event) – a mouse button is released. void mouseClicked(MouseEvent event) – a mouse button is pressed down and released without moving the mouse in between. void mouseEntered(MouseEvent event) – a mouse pointer is moved onto (over) a component. void mouseExited(MouseEvent event) – a mouse pointer is moved out of a component.
Methods in MouseMotionListener interface: void mouseMoved(MouseEvent event) – a mouse is moved. void mouseDragged(MouseEvent event) – a mouse is moved while the mouse button is pressed down.
To add a listener object that implements MouseListener interface a component, addMouseListener() method is used as: component.addMouseListener(listener); To add a listener object that implements MouseMotionListener interface to a component, addMouseMotionListener() method is used as: component.addMouseMotionListener(listener); Adapter classes (MouseAdapter class and MouseMotionAdapter class):
Suppose a class “Listener1” needs to be defined to implements MouseListener interface.
Even if we want to use only one method, say mousePressed method, we need to provide a definition with { and } for the rest of the methods for that class so that it compiles.
For example: public class Listener1 implements MouseListener
{
private int count = 0; public void mousePressed(MouseEvent event)
{
count++;
}
public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {}
}
Instead of using MouseListener interface, we can make a class inherit from MouseAdapter class.
MouseAdapter class implements the MouseListener interface and contains empty definitions for all methods of MouseListener. public class Listener1 extends MouseAdapter
{
private int count = 0; public void mousePressed(MouseEvent event)
{
count++;
}
}
For MouseMotionListener, we have MouseMotionAdapter class.
The Point class -- defined in java.awt package.
An object of the Point class represents the (x,y) coordinate of a given point in two dimensional space.
This can be used together with MouseEvent objects as:
Suppose that “event” is a MouseEvent object. Then, public void mousePressed(MouseEvent event)
{
Point pt = event.getPoint();
System.out.println(“X-coordinate is: “ + pt.x + “ and Y-coordinate is: “ + pt.y);
}
Here we can obtain the coordinate of the point where a user pressed using a mouse.
A Point object has x and y as its public variables, so we can access them directly.
An Example using MouseListener and Graphics
In this example, we use MouseListener to check the location of the panel where a user pressed using a mouse, then we draw a green dot at that location. The background of this panel is black.
It counts how many times a user pressed the panel (or how many dots are drawn so far). Here we see two approaches.
Approach – we can define paint or paintComponent method and use its Graphics object parameter to draw shapes.
//*******************************************
// Dots1.java
// Demonstrates mouse events and drawing on a panel.
//*******************************************
import javax.swing.JApplet; public class Dots1 extends JApplet
{
public void init()
{
getContentPane().add (new DotsPanel1()); setSize(300,200); }
}
Approach - defining