Write a Java program for handling mouse and key events.

Program Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/*<applet code="MouseDemo" width=400 height=400> </applet>*/
public class MouseDemo extends Applet implements MouseListener,MouseMotionListener
{
String msg=" ";
int mx=0,my=0;
public void init()
{
setBackground(Color.white);
setForeground(Color.red);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me){
mx=0;my=10;
msg="mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me){
mx=0;my=10;
msg="mouse entered";
repaint();
}
public void mouseExited(MouseEvent me){
mx=0;my=10;
msg="mouse exited";
repaint();
}
public void mousePressed(MouseEvent me){
mx=me.getX();
my=me.getY();
msg="Down";
repaint();
}
public void mouseReleased(MouseEvent me){
mx=me.getX();
my=me.getY();
msg="Up";
repaint();
}
public void mouseDragged(MouseEvent me){
mx=me.getX();
my=me.getY();
msg="*";
showStatus("moving mouse at"+me.getX()+","+me.getY());
}
public void mouseMoved(MouseEvent me){
showStatus("mouse is moving");
}
public void paint(Graphics g){
g.drawString(msg,mx,my);
}
}
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: