CSCI2014 Software Construction with JavaExercises: Mouse Events and Listeners

Exercises: Mouse Events and Listeners

Objectives

To detect and process mouse events.

Reading

Horstmann, Big Java: See sections 10.1 and 10.2 in the chapter on Event Handling.

Introduction

A mouse is an input device that is used extensively with Graphical User Interfaces. Many applications are simplified to “point and click” user interfaces. Examples of mouse events are: movements, pressed, released, clicked, multiple clicks, and dragging.

In order to detect a mouse event a component must associate itself with a mouse listener object. The interface javax.swing.event.MouseInputListener specifies the methods that are triggered by various mouse events. (Note that this interface is an amalgam of the java.awt MouseListener and MouseMotionListener interfaces.

Because there are several methods in this interface, programmers usually extend the MouseInputAdapter class. This class has empty bodies for all of the methods,so that a programmer only needs to override those of interest.

public abstract class MouseInputAdapter implements MouseInputListener

The methods in this class are empty; this class is provided as a convenience for easily creating listeners

by extending this class and overriding only the methods of interest.

public void mouseClicked(MouseEvent e)

Invoked when the mouse button has been clicked (pressed and released) on a component.

public void mouseEntered(MouseEvent e)

Invoked when the mouse enters a component.

public void mouseExited(MouseEvent e)

Invoked when the mouse exits a component.

public void mousePressed(MouseEvent e)

Invoked when a mouse button has been pressed on a component.

public void mouseReleased(MouseEvent e)

Invoked when a mouse button has been released on a component.

public void mouseDragged(MouseEvent e)

Invoked when a mouse button is pressed on a component and then dragged.

public void mouseMoved(MouseEvent e)

Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.

A MouseEvent is passed to the listener methods. This event carries information such as:

  • the (x, y) coordinates giving the mouse position where the event took place;
  • which mouse button was used;
  • the number of clicks;
  • the parent component using the getSource() method inherited from EventObject.

A selection of methods from class java.awt.event.MouseEvent ….

public int getButton()

Returns which, if any, of the mouse buttons has changed state.

public Point getPoint()

Returns the x,y position of the event relative to the source component.

public int getX()

Returns the horizontal x position of the event relative to the source component.

public int getY()

Returns the vertical y position of the event relative to the source component

public Object getSource()

Returns the object on which the event initially occurred. (Inherited from EventObject.)

Exercises

1This question tests all the mouse listener methods. Refer to the MyGraphicsComponent class from week 7.

a)Implement a class MyMouseHandler which extends MouseInputAdapter. Override each method, by providing a System.out.println() statement to output the name of the method and the coordinates of the mouse event.

import java.awt.event.*;

import javax.swing.event.*;

class MyMouseHandler extends MouseInputAdapter {

public void mouseClicked( MouseEvent e) {

int x = e.getX();

int y = e.getY();

System.out.println(“Clicked at (“ + x + “,” + y + “)” );

}

etc

}

b)In the constructor of the MyGraphicsComponent class use an instance of the MyMouseHandler class to associate a MouseListener and MouseMotionListener with the component. I.e.

class MyGraphicsComponent extends JComponent {

... attributes ...

public MyGraphicsComponent() {

...

MyMouseHandler mh = new MyMouseHandler();

this.addMouseListener(mh);

this.addMouseMotionListener(mh);

...

}

...

}

c)Compile and execute the program to see whether the events are detected, when you use the mouse.

2

a)Modify the MyGraphicsComponent class:

  • Declare two Point attibutes named p1 and p2, and initialize them in the constructor.

class MyGraphicsComponent extends JComponent {

...

private Point p1, p2;

public MyGraphicsComponent() {

...

p1 = new Point(0,0);

p2 = new Point(0,0);

}

  • Provide a setNextPoint(Point p) method which copies p2 to p1, and p to p2, and then calls repaint().

public void setNextPoint(Point p) {

p1 = p2;

p2 = p;

this.repaint(); //requests the component to refresh

}

  • Modify the paintComponent() method to draw a line from p1 to p2.

b)Modify the mouseClicked method in the mouse handler class so that it calls setNextPoint on your graphics component. Note that the handle back to the component may be obtained using the getSource() method.

public void mouseClicked(MouseEvent e) {

MyGraphicsComponent gc = (MyGraphicsComponent) e.getSource();

gc.setNextPoint(e.getPoint());

}

When you piece all this together the graphics component should display a line between the positions of the last two mouse clicks.

3Modify the previous program so that the line is initialized when the mouse is pressed, and continually updated while the mouse is dragged, until it is released. You may need additional set methods for p1 and p2 in the graphics component.

lz/csci2014/Week9Exercises2.doc/nov03Page 1 of 3