This page was saved using WebZIP 6.0.8.918 (Unregistered) on 01/20/05 오후 3:27:46.
Address: http://www.leepoint.net/notes-java/45examples/50mouse/10mousetest.html
Title: Java: Example - MouseTest.java  •  Size: 6276  •  Last Modified: Fri, 14 Jan 2005 00:25:28 GMT

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.
// MouseTest.java - Mouse event example
// Fred Swartz 2000-11-29...2002-11-21
// Possible enhancements: Show coordinates of the other events.

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


//////////////////////////////////////////////////////// class MouseTest  
public class MouseTest { 

    //====================================================== method main
    public static void main(String[] args) {
        JFrame windo = new MouseTestWindow();
        windo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        windo.setVisible(true);
    }//end main
}//endclass MouseTest



////////////////////////////////////////////////// class MouseTestWindow
class MouseTestWindow extends JFrame {

    //====================================================== constructor
    public MouseTestWindow() {
        //--- 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, 
                                                   "MousePanel 1"));
        mp2.setBorder(BorderFactory.createTitledBorder(etched,
                                                   "MousePanel 2"));
        
        //--- Layout the panels
        Container content = this.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
        content.add(mp1);
        content.add(mp2);
        
        this.setTitle("Mouse Test");
        this.pack();       // finalize layout
    }//end constructor
}//end MouseTest class
   
   

/////////////////////////////////////////////////////// class MousePanel
    // This JPanel listens to all mouse events on it, and draws.
class MousePanel extends JPanel 
                 implements MouseListener, MouseMotionListener {

   //================================================ instance variables
    private int xClicked = 0;  // x coord of mouse click 
    private int yClicked = 0;  // y coord of mouse click
    private int xMoved   = 0;  // x coord of mouse move 
    private int yMoved   = 0;  // y coord of mouse move
    //end instance variables
   

    //====================================================== constructor
    public MousePanel() {
        this.setBackground(Color.white);
        this.setPreferredSize(new Dimension(300, 300));
        //--- Add the mouse listeners.
        this.addMouseListener(this);       // listen to mouse events
        this.addMouseMotionListener(this); // listen to moves and drags
    }//endconstructor
    

    //============================================ method paintComponent
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // paint background and borders
        g.drawString("Last click: x=" + xClicked + ", y=" + yClicked
                                                          , 10, 30);
        g.drawString("x=" + xMoved   + ", y=" + yMoved 
                                                  , xMoved, yMoved);
    }//end paintComponent


    //============================================ listener mouseClicked
    public void mouseClicked(MouseEvent e) {
        xClicked = e.getX(); // save the x coordinate of the click
        yClicked = e.getY(); // save the y coordinate of the click
        this.repaint();      // paint the panel with the new values.
    }//end mouseClicked
  

    //============================================== listener mouseMoved
    public void mouseMoved(MouseEvent e) {
        xMoved = e.getX();
        yMoved = e.getY();
        this.repaint();
    }//end mouseMoved
  
  
    //========================================================== ignored
    //==== the other motion events must be here, but do nothing.
    public void mouseDragged (MouseEvent e) {}  // ignore
    //==== these "slow" mouse events are also 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
}//end class MousePanel
The text from the above example can be selected, copied, and pasted into an editor. The boxes will be ignored.
Sections that are colored orange are executed at run time.