Thursday, 1 January 2015

MouseListener in Java


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseListenerDemo extends Applet implements MouseListener,MouseMotionListener
{
    String s = "";
    int x = 0,y = 0;

    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    //MouseListener method
    public void mouseClicked(MouseEvent me)
    {
        x = me.getX();
        y = me.getY();
        showStatus("Mouse Clicked.");
        repaint();
    }

    //MouseListener method
    public void mousePressed(MouseEvent me)
    {
        showStatus("Mouse Pressed.");
        repaint();
    }

     //MouseListener method
     public void mouseReleased(MouseEvent me)
     {
        showStatus("Mouse Released.");
        repaint();
     }

    //MouseListener method
    public void mouseEntered(MouseEvent me)
    {
        showStatus("Mouse Entered.");
        repaint();
    } 

    //MouseListener method
    public void mouseExited(MouseEvent me)
    {
        showStatus("Mouse Exited.");
        repaint();
    }

    //MouseMotionListener method
    public void mouseMoved(MouseEvent me)
    {
        x = me.getX();
        y = me.getY();
        showStatus("Mouse Moved.");
        repaint();
    }

    //MouseMotionListener method
    public void mouseDragged(MouseEvent me)
    {
        x = me.getX();
        y = me.getY();
        showStatus("Mouse Dragged.");
        repaint();
    }

    public void paint(Graphics g)
    {
        s = "Moving mouse at " + x + "," + y;
        g.drawString(s,10,10);
    }
}

/*


*/
Output

No comments:

Post a Comment