MOUSE HANDLING

CONTENTS

1. Introduction
2. Mouse coordinates (mouse listener example)
2. Mouse moved example



1. INTRODUCTION

The most obvious mechanism for interacting with GUIs is via the mouse. Actions such a clicking, releasing and pressing mouse buttons all generate events that are detected by the mouse listener. Actions such as moving or dragging the mouse are detected by the mouse motion listener.




2. MOUSE COORDINATES (MOUSE LISTENER EXAMPLE)

It is often desirable to "know" the coordinates of mouse clicks within a conatiner. This can be achieved using the getX and getY methods in the MouseEvent class. Some example code that uses these two methods is given in Table 1 (the code is founded on a similar example presented in Holmes 1998). Note that when a mouse button is pressed this then invokes the mousePressed method contained in the WindowListener interface. Because this is an interface we need to implement this method in our code, we also need to provide rudimentry implmentations for the other methods contained in this interface.

// MOUSE EXAMPLE PROGRAM
// Frans Coenen
// Tuesday 2nd January 2001
// Dept. of Comp. Sci., University of Liverpool

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

class MouseEx extends Frame implements WindowListener, MouseListener {

    // ----------------------- FIELDS --------------------------
    
    /* None */
     
    // --------------------- CONSTRUCTORS ----------------------
    
    public MouseEx(String text) {
        super(text);
        setBackground(Color.orange);
        addWindowListener(this);
        addMouseListener(this);
	} 

    // ---------------------- ACTION MOUSE LISTENERS -------------------

    /* Blank mouse listener methods  */
    
    public void mouseClicked(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}
	
    /* Mouse pressed */
    
    public void mousePressed(MouseEvent event) {
        // Get coordinates
	
	int xCoord = event.getX();
	int yCoord = event.getY();
	
	Graphics g = getGraphics();
	
	// Display message on screen
	g.drawString("+ [" + String.valueOf(xCoord) + "," + String.valueOf(yCoord) + "]",
			xCoord,yCoord);
	}

    // ---------------------- ACTION WINDOW LISTENERS -------------------
    
    /* Blank methods for window listener */
    
    public void windowClosed(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}
    public void windowOpened(WindowEvent event) {}

    /* Window Closing */
    
    public void windowClosing(WindowEvent event) {
        System.exit(0);
        } 
    }
    
/* --------------------------------------------------------------- */
/*                                                                 */
/*                            MOUSE APP                            */
/*                                                                 */
/* --------------------------------------------------------------- */
    
class MouseExApp {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        MouseEx screen = new MouseEx("Mouse example");
        
        screen.setSize(400,300);
        screen.setVisible(true);
        }
    } 

Table 1:Mouse example

The resulting GUI is shown in Figure 1.

MOUSE EXAMPLE

Figure 1: Result of exacuting code presented in Table 1.




3. MOUSE COORDINATES (MOUSE LISTENER EXAMPLE)

We can write some similar code that detects mouse movements using the mouse motion listener methods:

  1. mouseMoved detects mouse motion.
  2. mouseDragged detects mouse motion with when mouse button depressed.

Some example code is given in Table 2.

// MOUSE EXAMPLE PROGRAM
// Frans Coenen
// Tuesday 2nd January 2001
// Dept. of Comp. Sci., University of Liverpool

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

class MouseDragEx extends Frame implements WindowListener, MouseMotionListener {

    // ----------------------- FIELDS --------------------------
    
    //int xCoord, yCoord;
    TextField position = new TextField();
     
    // --------------------- CONSTRUCTORS ----------------------
    
    public MouseDragEx(String text) {
        super(text);
        setBackground(Color.yellow);
	setLayout(null);
	
	// Set up contents
	
	setUpPositionTextField();
	
	// Add listeners
	
        addWindowListener(this);
	addMouseMotionListener(this);
	} 
    
    // ---------------------- ACTION MOUSE MOVED LISTENERS -------------------

    /* Mouse moved */
    
    public void mouseMoved(MouseEvent event) {
        // Display message on screen
	
	position.setText("[" + String.valueOf(event.getX()) + ", " + 
		String.valueOf(event.getY()) + "]  ");
        }

    /* Mouse dragged */
        
    public void mouseDragged(MouseEvent event) {
        Graphics g = getGraphics();
	
	// Display message on screen
	position.setText("[" + String.valueOf(event.getX()) + ", " + 
		String.valueOf(event.getY()) + "]  ");
	}
	
    // ---------------------- ACTION WINDOW LISTENERS -------------------
    
    /* Blank methods for window listener */
    
    public void windowClosed(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}
    public void windowOpened(WindowEvent event) {}

    /* Window Closing */
    
    public void windowClosing(WindowEvent event) {
        System.exit(0);
        } 

   /* ---------------------------- METHODS ------------------------- */ 
   
   private void setUpPositionTextField() {
       position.setLocation(10,30);
       position.setSize(85,30);
       position.setBackground(Color.white);
       add(position);
       }   
    }
    
/* --------------------------------------------------------------- */
/*                                                                 */
/*                            MOUSE APP                            */
/*                                                                 */
/* --------------------------------------------------------------- */
    
class MouseDragExApp {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        MouseDragEx screen = new MouseDragEx("Mouse Drag example");
        
        screen.setSize(400,300);
        screen.setVisible(true);
        }
    } 

Table 2:Mouse motion example



REFERENCES

  1. Holmes, B. (1998). Programming with Java. Jones and Bartlett, London.



Created and maintained by Frans Coenen. Last updated 08 January 2001