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);
}
}
Post A Comment:
0 comments: