m_event



->The following code illustrates working of mouse Events in java .
->The color of applet window changes when mouse is entered,exited,pressed,released,clicked.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="change.class" width=600 height=600>
</applet>*/
public class change extends Applet implements MouseListener
{
int mx=0,my=0;
public void init()
{
addMouseListener(this);
}
public void mouseEntered(MouseEvent me)
{
mx=10;my=10;
setBackground(Color.cyan);
}
public void mouseExited(MouseEvent me)
{
mx=20;my=20;
setBackground(Color.red);
}
public void mouseClicked(MouseEvent me)
{
mx=30;my=30;
setBackground(Color.yellow);
}
public void mousePressed(MouseEvent me)
{
mx=40;my=40;
setBackground(Color.blue);
}
public void mouseReleased(MouseEvent me)
{
mx=50;my=50;
setBackground(Color.green);
}
public void paint(Graphics g)
{
g.drawString("Actionperformed",mx,my);      //The string action performed will be displayed on applet                                                                                  window.    
}
}

No comments:

Post a Comment