Program to demonstrate the Mouse events using Applet

In this program, we have to implement Mouse events. An event which indicates that a mouse action is occurred in a component. This event is used both for mouse events (enter, click, press, release, exit) and mouse motion events (moves, drags). 




Here we have to use MouseListener and MouseMotionListener interfaces of Java's java.awt.event package. The MouseListener interface is used for receiving mouse events (includes click, press, release, enter and exit) on a component. The MouseMotion Listener interface is used for receiving mouse motion events (includes moves and drags) on a component.


Whenever you implement MouseListener interface you must need to override the following method:

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

and for MouseMotionListener you must need to override the following methods:

public void mouseMoved(MouseEvent me)
public void mouseDragged(MouseEvent me)



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

/* <APPLET CODE="MouseEventsDemo.class" WIDTH="300" HEIGHT="300">
   </APPLET>
*/

public class MouseEventsDemo extends Applet implements MouseListener, MouseMotionListener {

 String message = "";
 
 public void init() {
  
  setBackground(Color.YELLOW);
  addMouseListener(this);
  addMouseMotionListener(this);
 }
 
 public void paint(Graphics g) {
  g.drawString(message, 50, 50);
 }
 
 public void mouseEntered(MouseEvent me) {
  setBackground(Color.PINK);
  message = "Mouse Entered: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
 
 public void mouseExited(MouseEvent me) {
  setBackground(Color.RED);
  message = "Mouse Exited: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
 
 public void mouseClicked(MouseEvent me) {
  setBackground(Color.CYAN);
  message = "Mouse Clicked: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
 
 public void mousePressed(MouseEvent me) {
  setBackground(Color.MAGENTA);
  message = "Mouse Pressed: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
 
 public void mouseReleased(MouseEvent me) {
  setBackground(Color.GREEN);
  message = "Mouse Released: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
 
 public void mouseMoved(MouseEvent me) {
  setBackground(Color.ORANGE);
  message = "Mouse Moved: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
 
 public void mouseDragged(MouseEvent me) {
  setBackground(Color.GRAY);
  message = "Mouse Dragged: (" + me.getX() + ", " + me.getY() + ")";
  repaint();
 }
}
OUTPUT
C:\>javac MouseEventsDemo.java
C:\>appletviewer MouseEventsDemo.java

MouseEventsDemo.java OUTPUT

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet