Mouse Listener

mouseEntered
mouseExited
mousePressed
mouseReleased
mouseClicked

Applet :     Application displays three canvas with Red, Green and Cyan color. On selecting particular canvas the background color is changed to that canvas color.










import java.awt.*;
import java.awt.event.*;
public class FrameMouseCanvas extends Applet implements MouseListener
{
    Canvas c1,c2,c3;
    public void init()
    {
        c1=new Canvas();
        c2=new Canvas();
        c3=new Canvas();

        add(c1);
        add(c2);
        add(c3);
       
        c1.setSize(100,100);
        c2.setSize(100,100);
        c3.setSize(100,100);

        c1.setBackground(Color.red);
        c2.setBackground(Color.green);
        c3.setBackground(Color.cyan);

        c1.addMouseListener(this);
        c2.addMouseListener(this);
        c3.addMouseListener(this);
    }
    public void mouseEntered(MouseEvent e)
    {
        if(e.getSource()==c1)
        {
            setBackground(Color.red);
        }
        else if(e.getSource()==c2)
        {
            setBackground(Color.green);
        }
        else if(e.getSource()==c3)
        {
            setBackground(Color.cyan);
        }
    }
    public void mouseExited(MouseEvent e)
    {
        if(e.getSource()==c1||e.getSource()==c2||e.getSource()==c3)
        {
            setBackground(Color.white);
        }
    }
    public void mousePressed(MouseEvent e)
    {
    }
    public void mouseClicked(MouseEvent e)
    {
    }
    public void mouseReleased(MouseEvent e)
    {
    }
}

Click new -> File -> html    and save this file as FrameMouseCanvas.html

<BODY>

<Applet code=FrameMouseCanvas.class height=200 width=400 >

</Applet>

</BODY>


Application :     In first TextArea user perform any MouseAction, simultaneously the action name should appear in the second TextArea along with the location(row and column number).











import java.awt.*;
import java.awt.event.*;
class FrameMouseAction implements MouseListener
{
    Frame f;
    TextArea ta1, ta2;

    FrameMouseAction()
    {
        f = new Frame();
        ta1=new TextArea(20,20);
        ta2=new TextArea(20,20);
   
        f.add(ta1);
        f.add(ta2);
       
        f.setLayout(new FlowLayout());
        ta1.addMouseListener(this);

        f.setSize(400,400);
        f.setVisible(true);
    }
    public void mouseEntered(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        ta2.append("Entered"+" at "+x+","+y+"\n");
    }
    public void mouseExited(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        ta2.append("Exited"+" at "+x+","+y+"\n");
    }
    public void mousePressed(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        ta2.append("Pressed"+" at "+x+","+y+"\n");
    }
    public void mouseClicked(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        ta2.append("Clicked"+" at "+x+","+y+"\n");
    }
    public void mouseReleased(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        ta2.append("Released"+" at "+x+","+y+"\n");
    }
    public static void main(String [] args)
    {
        FrameMouseAction x = new FrameMouseAction();
    }
}