Skip to main content

JAVA MouseListener, MouseMotionListener

Java MouseListener Interface

The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods.

Methods of MouseListener interface

The signature of 5 methods found in MouseListener interface are given below:

  1. public abstract void mouseClicked(MouseEvent e);  
  2. public abstract void mouseEntered(MouseEvent e);  
  3. public abstract void mouseExited(MouseEvent e);  
  4. public abstract void mousePressed(MouseEvent e);  
  5. public abstract void mouseReleased(MouseEvent e);  

Java MouseListener Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseListenerExample extends Frame implements MouseListener{  
  4.     Label l;  
  5.     MouseListenerExample(){  
  6.         addMouseListener(this);  
  7.           
  8.         l=new Label();  
  9.         l.setBounds(20,50,100,20);  
  10.         add(l);  
  11.         setSize(300,300);  
  12.         setLayout(null);  
  13.         setVisible(true);  
  14.     }  
  15.     public void mouseClicked(MouseEvent e) {  
  16.         l.setText("Mouse Clicked");  
  17.     }  
  18.     public void mouseEntered(MouseEvent e) {  
  19.         l.setText("Mouse Entered");  
  20.     }  
  21.     public void mouseExited(MouseEvent e) {  
  22.         l.setText("Mouse Exited");  
  23.     }  
  24.     public void mousePressed(MouseEvent e) {  
  25.         l.setText("Mouse Pressed");  
  26.     }  
  27.     public void mouseReleased(MouseEvent e) {  
  28.         l.setText("Mouse Released");  
  29.     }  
  30. public static void main(String[] args) {  
  31.     new MouseListenerExample();  
  32. }  
  33. }  

Output:

java awt mouselistener example 1

Java MouseListener Example 2

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseListenerExample2 extends Frame implements MouseListener{  
  4.     MouseListenerExample2(){  
  5.         addMouseListener(this);  
  6.           
  7.         setSize(300,300);  
  8.         setLayout(null);  
  9.         setVisible(true);  
  10.     }  
  11.     public void mouseClicked(MouseEvent e) {  
  12.         Graphics g=getGraphics();  
  13.         g.setColor(Color.BLUE);  
  14.         g.fillOval(e.getX(),e.getY(),30,30);  
  15.     }  
  16.     public void mouseEntered(MouseEvent e) {}  
  17.     public void mouseExited(MouseEvent e) {}  
  18.     public void mousePressed(MouseEvent e) {}  
  19.     public void mouseReleased(MouseEvent e) {}  
  20.       
  21. public static void main(String[] args) {  
  22.     new MouseListenerExample2();  
  23. }  
  24. }  

Output:

java awt mouselistener example 2

ava 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:

java awt mousemotionlistener example 1


Java MouseMotionListener Example 2

  1. import java.awt.*;  
  2. import java.awt.event.MouseEvent;  
  3. import java.awt.event.MouseMotionListener;  
  4. public class Paint extends Frame implements MouseMotionListener{  
  5.     Label l;  
  6.     Color c=Color.BLUE;  
  7.     Paint(){  
  8.     l=new Label();  
  9.     l.setBounds(20,40,100,20);  
  10.     add(l);  
  11.       
  12.     addMouseMotionListener(this);  
  13.       
  14.     setSize(400,400);  
  15.     setLayout(null);  
  16.     setVisible(true);  
  17. }  
  18. public void mouseDragged(MouseEvent e) {  
  19.     l.setText("X="+e.getX()+", Y="+e.getY());  
  20.     Graphics g=getGraphics();  
  21.     g.setColor(Color.RED);  
  22.     g.fillOval(e.getX(),e.getY(),20,20);  
  23. }  
  24. public void mouseMoved(MouseEvent e) {  
  25.     l.setText("X="+e.getX()+", Y="+e.getY());  
  26. }  
  27. public static void main(String[] args) {  
  28.     new Paint();  
  29. }  
  30. }  

Output:

java awt mousemotionlistener example 2


Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...