Write a java program that handles all mouse events and shows the event name at the center of the window when a mouse event is fired (use Adapter classes).

Program Code:
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JApplet;
public class mouseevnts extends JApplet implements MouseListener
{
private int x,y;
private String event;
public void init()
{
setLayout(new FlowLayout());
x=-1;
addMouseListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(0,0,getWidth(),getHeight());
if(x!=1)
{
g.drawString("Mouseevent is"+event+"("+x+","+y+")", 10,50);
}
}
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
event="pressed";
repaint();
}
public void mouseClicked(MouseEvent e)
{
x=e.getX();
y=e.getY();
event="clicked";
repaint();
}
public void mouseReleased(MouseEvent e)
{
x=e.getX();
y=e.getY();
event="Reeleased";
repaint();
}
public void mouseExited(MouseEvent e)
{
x=e.getX();
y=e.getY();
event="Exited";
repaint();
}
public void mouseEntered(MouseEvent e)
{
x=e.getX();
y=e.getY();
event="Entered";
repaint();
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: