Mouse Event, Java Program, Event Handling
Write a Java Program to demonstrate Mouse events.

Program Code:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/* <applet code="MouseEvents.class" width=300 height=200> </applet> */
public class MouseEvents extends Applet implements MouseListener,MouseMotionListener
{
String msg =" ";
int x=0,y=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent m)
{
x=10;
y=10;
msg ="mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=10;
y=10;
msg ="mouse Entered";
repaint();
}
public void mouseExited(MouseEvent m)
{
x=10;
y=10;
msg ="mouse Exited";
repaint();
}
public void mousePressed(MouseEvent m)
{
x=m.getX();
y=m.getY();
msg ="Down";
repaint();
}
public void mouseReleased(MouseEvent m)
{
x=m.getX();
y=m.getY();
msg ="Up";
repaint();
}
public void mouseDragged(MouseEvent m)
{
x=m.getX();
y=m.getY();
msg ="*";
showStatus("Draggeg mouse at " +x+ " & "+y);
repaint();
}
public void mouseMoved(MouseEvent m)
{
showStatus("Moving mouse at " +m.getX()+ " & "+m.getY());
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
}
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: