12/22/2011

Java applet (mouse events)

Java applet (mouse events)
Java applet (mouse events)


/*Java applet (mouse events)*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MouseActions extends Applet implements MouseListener,MouseMotionListener
{
int mouseX=0,mouseY=0; //Coordinates
String msg="";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.blue);
setForeground(Color.yellow);
}
public void mouseClicked(MouseEvent me)
{
//mouseX=30;
//mouseY=30; 
mouseX=me.getX();
mouseY=me.getY();
msg="Mouse is clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=30;
mouseY=30;
msg="Mouse is entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=30;
mouseY=30;
msg="Mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
//mouseX=me.getX(); //Mouse event
//mouseY=me.getY();
mouseX=30;
mouseY=30;
msg="Mouse is pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=me.getX(); //Mouse event
mouseY=me.getY();
msg="Mouse is Released";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX(); //Mouse event
mouseY=me.getY();
msg="*";
showStatus("Mouse is dragged at X: " + mouseX + " , Y: " + mouseY);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("Mouse is moving at X: " + me.getX() + " , Y: " + me.getY());

}
public void paint(Graphics g)
{
Font f = new Font("Arial",Font.BOLD,25); 
g.setFont(f); 
g.drawString(msg,mouseX,mouseY);
} 

} 
//<applet code="MouseActions.class" height=500 width=500></applet>

No comments:

Post a Comment