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...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...

Genotype Representation

Binary Representation This is one of the simplest and most widely used representation in GAs. In this type of representation the genotype consists of bit strings. For some problems when the solution space consists of Boolean decision variables – yes or no, the binary representation is natural. Take for example the 0/1 Knapsack Problem. If there are n items, we can represent a solution by a binary string of n elements, where the x th  element tells whether the item x is picked (1) or not (0). For other problems, specifically those dealing with numbers, we can represent the numbers with their binary representation. The problem with this kind of encoding is that different bits have different significance and therefore mutation and crossover operators can have undesired consequences. This can be resolved to some extent by using  Gray Coding,  as a change in one bit does not have a massive effect on the solution. Real Valued Representation For problems where we want to define ...