Java: Example - MouseTest.java

This is a simple demonstration of listening to mouse events on a panel. This displays two panels to show how the mouse listener depends on the component.

The main program / applet

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
// File:   mousedeme/MouseTest.java
// Description: Main program/applet to demo mouse listeners.
// Author: Fred Swartz
// Date:   2005-02-03, 2000-11-29...2002-11-21
// Possible enhancements: Show other mouse events.


package mousedemo;
import javax.swing.*;

//////////////////////////////////////////////////////// class MouseTest  
public class MouseTest extends JApplet{ 
    //===================================================== constructor
    public MouseTest() {
        add(new DualMousePanel()); 
    }
    
    //====================================================== method main
    public static void main(String[] args) {
        JFrame window = new JFrame("Mouse Demo");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(new DualMousePanel());
        window.pack();
        window.setVisible(true);
    }
}

The content pane of the GUI

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
// File:   mousedeme/MousePanel.java
// Description: Panel holding two MousePanels.
// Author: Fred Swartz
// Date:   2005-02-03, 2000-11-29...2002-11-21

package mousedemo;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

////////////////////////////////////////////////// class DualMousePanel
class DualMousePanel extends JPanel {
    //====================================================== constructor
    public DualMousePanel() {
        //--- Create two MousePanels
        MousePanel mp1 = new MousePanel();
        MousePanel mp2 = new MousePanel();
        
        //--- Add borders (note: borders are inside panel)
        Border etched = BorderFactory.createEtchedBorder();
        mp1.setBorder(BorderFactory.createTitledBorder(etched, "Panel 1"));
        mp2.setBorder(BorderFactory.createTitledBorder(etched, "Panel 2"));
        
        //--- Layout the panels
        this.setLayout(new GridLayout(1, 2));
        this.add(mp1);
        this.add(mp2);
    }//end constructor
}

The panel which listens to and shows mouse events

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
// File:   mousedeme/MousePanel.java
// Description: Panel that listens to mouse events and displays info
//              about some of them.
// Author: Fred Swartz
// Date:   2005-02-03, 2000-11-29...2002-11-21

package mousedemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/////////////////////////////////////////////////////// class MousePanel
class MousePanel extends JPanel implements MouseListener, MouseMotionListener {
    
   //================================================ instance variables
    private int m_lastClickedX = 0;  // x coord of mouse click 
    private int m_lastClickedY = 0;  // y coord of mouse click
    private int m_lastMovedX   = 0;  // x coord of mouse move 
    private int m_lastMovedY   = 0;  // y coord of mouse move
    
   
    //====================================================== constructor
    public MousePanel() {
        this.setBackground(Color.white);
        this.setPreferredSize(new Dimension(200, 200));
        //--- Add the mouse listeners.
        this.addMouseListener(this);       // listen to mouse events
        this.addMouseMotionListener(this); // listen to moves and drags
    }
    
    
    //============================================ method paintComponent
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // paint background and borders
        g.drawString("Last click: x=" + m_lastClickedX 
                      + ", y=" + m_lastClickedY , 10, 30);
        g.drawString("x=" + m_lastMovedX   + ", y=" + m_lastMovedY 
                      , m_lastMovedX, m_lastMovedY);
    }
    

    //============================================ listener mouseClicked
    public void mouseClicked(MouseEvent e) {
        m_lastClickedX = e.getX(); // Save the x coordinate of the click
        m_lastClickedY = e.getY(); // Save the y coordinate of the click
        this.repaint();            // Paint the panel with the new values.
    }
    
  
    //============================================== listener mouseMoved
    public void mouseMoved(MouseEvent e) {
        m_lastMovedX = e.getX();
        m_lastMovedY = e.getY();
        this.repaint();
    }
  
  
    //========================================================== ignored
    //==== the other motion events must be here, but do nothing.
    public void mouseDragged (MouseEvent e) {}  // ignore
    //==== these "slow" mouse events are ignored.
    public void mouseEntered (MouseEvent e) {}  // ignore
    public void mouseExited  (MouseEvent e) {}  // ignore
    public void mousePressed (MouseEvent e) {}  // ignore
    public void mouseReleased(MouseEvent e) {}  // ignore
}