EEK! A Mouse Event!

Last week, we built a little program that monitored windows for clicks

and drags of the mouse. We created a Canvas object that implemented the

appropriate listener classes and inserted the "listening Canvas" into

the window we wanted to monitor. The listeners told us when and where

clicks and drags took place.

This week, let's use the same general approach to monitor all

detectible mouse events, including:

* Clicks

* Drags

* Moves

* Button-presses

* Button-releases

* Entry into the monitored area

* Exit from the monitored area

Basically, we will override all the event handlers in the MouseListener

and MouseMotionListener classes and call a method called showEvent().

Using methods, showEvent() determines which mouse button has been

clicked, if any modifier keys (such as Ctrl) have been pressed

simultaneously, and displays events in text that appear in the window.

It's a long program, so here you go:

import java.awt.*;

import java.awt.event.*;

public class AllMouseEvents extends Frame {

public static void main(String[] arg) {

new AllMouseEvents();

}

AllMouseEvents() {

super("AllMouseEvents");

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e)

{ System.exit(0); } } );

AllMousePanel ap = new AllMousePanel();

add(ap);

pack();

show();

}

class AllMousePanel extends Canvas

implements MouseListener, MouseMotionListener {

String when = "";

String modifier = "";

String eventID = "";

String location = "";

String clickCount = "";

AllMousePanel() {

addMouseListener(this);

addMouseMotionListener(this);

setSize(400,150);

}

public void paint(Graphics g) {

g.drawString(modifier,20,30);

g.drawString(eventID,20,50);

g.drawString(location,20,70);

g.drawString(clickCount,20,90);

g.drawString(when,20,110);

}

public void mouseMoved(MouseEvent event) {

showEvent(event);

}

public void mouseDragged(MouseEvent event) {

showEvent(event);

}

public void mouseEntered(MouseEvent event) {

showEvent(event);

}

public void mouseExited(MouseEvent event) {

showEvent(event);

}

public void mouseClicked(MouseEvent event) {

showEvent(event);

}

public void mousePressed(MouseEvent event) {

showEvent(event);

}

public void mouseReleased(MouseEvent event) {

showEvent(event);

}

private void showEvent(MouseEvent event) {

modifier = "";

if(event.isAltDown())

modifier += "ALT ";

if(event.isControlDown())

modifier += "CTRL ";

if(event.isMetaDown())

modifier += "META ";

if(event.isShiftDown())

modifier += "SHIFT ";

if((event.getModifiers() & InputEvent.BUTTON1_MASK) != 0)

modifier += "BUTTON1 ";

if((event.getModifiers() & InputEvent.BUTTON2_MASK) != 0)

modifier += "BUTTON2 ";

if((event.getModifiers() & InputEvent.BUTTON3_MASK) != 0)

modifier += "BUTTON3 ";

switch(event.getID()) {

case MouseEvent.MOUSE_CLICKED:

eventID = "Mouse clicked";

break;

case MouseEvent.MOUSE_DRAGGED:

eventID = "Mouse dragged";

break;

case MouseEvent.MOUSE_ENTERED:

eventID = "Mouse entered";

break;

case MouseEvent.MOUSE_EXITED:

eventID = "Mouse exited";

break;

case MouseEvent.MOUSE_MOVED:

eventID = "Mouse moved";

break;

case MouseEvent.MOUSE_PRESSED:

eventID = "Mouse pressed";

break;

case MouseEvent.MOUSE_RELEASED:

eventID = "Mouse released";

break;

default:

eventID = "";

break;

}

clickCount = "Click count: " + event.getClickCount();

location = "(" + event.getX() + "," + event.getY() + ")";

when = "When: " + event.getWhen();

repaint();

}

}

}

What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies