//////////////////////////////////////////////////////// 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