This program tells about how to use action Listener methods and its methods along with how to handle the mouse events.whenever you perform such type of functions such as :click,released,drag,move,enter or exit from the window then it reflects your output as by the repaint method:
To Know more about the mouse event functions and its calculations check out the Java Label:
NewClass.java:
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
/*
<applet code="NewClass" width=300 height=100>
</applet>
*/
public class NewClass extends Applet implements MouseListener, MouseMotionListener
{
String msg=" ";
int mouseX=0,mouseY=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX= 0;
mouseY=10;
msg="mouse clicked at:"+me.getX()+" ' "+me.getY();
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{ mouseX=0;
mouseY=10;
msg="mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Realeased";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="***";
showStatus("Dragging mouse at "+ mouseX +" ,"+ mouseY);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("moving mouse at"+ me.getX() + ","+ me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
output: