Mouse and Mouse Motion Event

//MouseListener and MouseMotionListener
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseListenerDemo extends JApplet implements MouseListener,
		MouseMotionListener {
	String msg = "";
	int mouseX = 10, mouseY = 20;
	int movX = 0, movY = 0;

	@Override
	public void init() {
		addMouseListener(this);
		addMouseMotionListener(this);

		// MyWindowAdapter adepter=new MyWindowAdapter(this);
		// addWindowListener(adepter);
	}

	@Override
	public void mouseClicked(MouseEvent me) {

	}

	@Override
	public void mouseEntered(MouseEvent evtob) {
		mouseX = 10;
		mouseY = 54;
		msg = "mouse just entered child";
		repaint();
	}

	@Override
	public void mouseExited(MouseEvent evtobj) {
		mouseX = 10;
		mouseY = 54;
		msg = "mouse just left child window";
		repaint();
	}

	@Override
	public void mousePressed(MouseEvent me) {
		mouseX = me.getX();
		mouseY = me.getY();
		msg = "Down";
		repaint();
	}

	@Override
	public void mouseReleased(MouseEvent me) {
		mouseX = me.getX();
		mouseY = me.getY();
		msg = "Up";
		repaint();
	}

	@Override
	public void mouseDragged(MouseEvent me) {
		mouseX = me.getX();
		mouseY = me.getY();
		movX = me.getX();
		movY = me.getY();
		msg = "*";
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent me) {
		movX = me.getX();
		movY = me.getY();
		repaint(0, 0, 100, 60);
	}

	@Override
	public void paint(Graphics g) {
		g.drawString(msg, mouseX, mouseY);
		g.drawString("Mouse At" + movX + "," + movY, 10, 40);
	}

}

Download

Event handling in Java

Action Event with Menu Item
Action Event in Applet
Action Event
Adjustment Event
Focus Event
Item Event
Key Event
MOuse Event
Event with Timer
Window Event


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code