Practical 11: Write a program to demonstrate various mouse events using MouseListener and MouseMotionListener interface

1) Write a program to change the background color of Applet when user performs event using Mouse.

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

public class MouseColor extends Applet implements MouseMotionListener
{
    public void init()
    {
        addMouseMotionListener(this);
    }

    public void mouseDragged(MouseEvent me)
    {
        setBackground(Color.red);
        repaint();
    }

    public void mouseMoved(MouseEvent me)
    {
        setBackground(Color.green);
        repaint();
    }

}
/*
    <applet code="MouseColor" width=300 height=300>
    </applet>
*/




Output:
MouseMotionListener Color Change


2) Write a program to count the number of clicks performed by the user in a applet?

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

public class MouseCount extends Applet implements MouseListener
{
    int count = 0;
    public void init()
    {
        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent me)
    {
        count++;
        showStatus("Number of time Clicked:"+count);
        repaint();
    }

    public void mouseEntered(MouseEvent me)
    {
    }
    public void mouseExited(MouseEvent me)
    {
    }
    public void mousePressed(MouseEvent me)
    {
    }
    public void mouseReleased(MouseEvent me)
    {
    }
}
/*
    <applet code="MouseCount" width=300 height=300>
    </applet>
*/


Output:
MouseListener counts MouseClicked


Previous
Next Post »