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