Java Tutorial

AWT Mouse Operation

Mouse is the hardware component which always deals with user interactive with various active components. On mouse we can perform two types of operations, they are basic operations and advanced operations.

To deal with basic operations of mouse we must take an interface called MouseListener. This interface contains the following undefined methods:

public void mousePressed (MouseEvent); 
public void mouseClicked (MouseEvent); 
public void mouseExited (MouseEvent); 
public void mouseReleased (MouseEvent); 
public void mouseEntered (MouseEvent);

In order to perform the basic operations of the mouse we must have the following registration method which are present in component class.

public void addMouseListener (MouseListener);
public void removeMouseListener (MouseListener);

To perform advanced operations of mouse we must use a predefined interface called MouseMotionListener and it contains the following methods:

public void mouseDragged (MouseEvent); 
public void mouseMoved (MouseEvent);

In order to deal with advanced operations we must have the following registration methods:

public void addMouseMotionListener (MouseMotionListener); 
public void removeMouseMotionListener (MouseMotionListener);

Write a java program which illustrates the basic and advanced operations of mouse?

Answer:

import java.awt.*; 
import java.awt.event.*;
import java.applet.Applet;
/*<applet code="mouse" width=300 height=300>
</applet>*/
public class mouse extends Applet
{
    String msg; 
    int x,y;
    public void init (0
    {
        x=100; 
        y=100;
        msg=null;
        setBackground (Color.yellow); 
        setForeground (Color.green);
    }
    public void start ()
    {
        addMouseListener (new ml (this)); 
        addMouseMotionListener (new mml (this));
    }
    public void paint (Graphics g)
    {
        Font f=new Font ("verdana", Font.BOLD, 60); 
        g.getFont (f);
        g.drawString (msg+"("+x+","+y+")",x,y);
    }
    class ml implements MouseListener
    {
        mouse m;
        ml (mouse m)
        {
            this.m=m;
        }
        public void mouseEntered (MouseEvent me)
        {
            m.showStatus ("MOUSE ENTERED IN THE WINDOW");
        }
        public void mouseExited (MouseEvent me)
        {
            m.showStatus ("MOUSE EXITED FROM WINDOW");
        }
        public void mousePressed (MouseEvent me)
        {
            m.msg="Mouse Pressed"; 
            m.x=me.getX ();
            m.y=me.getY ();
            m.repaint ();
        }
        public void mouseReleased (MouseEvent me)
        {
            m.msg="Mouse Released"; 
            m.x=me.getX ();
            m.y=me.getY ();
            m.repaint ();
        }
        public void mouseClicked (MouseEvent me)
        {
            m.msg="Mouse Clicked"; 
            m.x=me.getX ();
            m.y=me.getY ();
            m.repaint ();
        }
        class mml extends MouseMotionAdapter
        {
            mouse m;
            mml (mouse m)
            {
                this.m=m;
            }
            public void mouseMoved (MouseEvent me)
            {
                m.msg="Mouse Moved"; 
                m.x=me.getX ();
                m.y=me.getY ();
                m.repaint ();
            }
            public void mouseDragged (MouseEvent me)
            {
                m.msg="Mouse Dragged"; 
                m.x=me.getX ();
                m.y=me.getY ();
                m.repaint ();
            }
        }
    }
};

Note:

java.applet.Applet;
public void showStatus (String); - 1 
public void repaint (); - 2

Method 1 is used for setting the status of applet window and Method 2 is used for calling the paint method automatically for number of times.