1import java.awt.BorderLayout;
2import java.awt.Color;
3import java.awt.Dimension;
4import java.awt.Graphics;
5import java.awt.GridLayout;
6import java.awt.event.MouseAdapter;
7import java.awt.event.MouseEvent;
8import java.awt.event.MouseMotionAdapter;
9
10import javax.swing.JApplet;
11import javax.swing.JComponent;
12import javax.swing.JFrame;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15
16public
17class MouseMotionApplet extends JApplet {
18 JPanel p;
19 public void init() {
20 p = new MyPanel();
21 getContentPane().add(p);
22 p.revalidate();
23 p.repaint();
24 }
25}
26
27class MyCanvas extends JPanel {
28 public MyCanvas() {
29 setSize(300,300);
30 setBackground(Color.blue);
31 }
32}
33
34class MyPanel extends JPanel {
35 JComponent c;
36 JLabel l;
37
38 public MyPanel() {
39 setSize(300, 330);
40 setLayout(null);
41
42 c = new MyCanvas();
43 c.setLocation(0,0);
44
45 l = new JLabel();
46 l.setSize(300,30);
47 l.setLocation(0,300);
48
49 add(c);
50 add(l);
51
52
53
54
55
56 c.addMouseMotionListener
57 (new MouseMotionAdapter() {
58 public
59 void mouseMoved(MouseEvent e) {
60 mouseStatus("mouse at",e.getX(),e.getY());
61 }
62 public
63 void mouseDragged(MouseEvent e) {
64 mouseStatus("mouse dragged at",e.getX(),e.getY());
65 }
66 }
67 );
68
69
70
71
72
73 c.addMouseListener
74 (new MouseAdapter() {
75 public
76 void mousePressed(MouseEvent e) {
77 mouseStatus("mouse pressed at",e.getX(),e.getY());
78 }
79 public
80 void mouseReleased(MouseEvent e) {
81 mouseStatus("mouse released at",e.getX(),e.getY());
82 }
83 public
84 void mouseEntered(MouseEvent e) {
85 mouseStatus("mouse entered at",e.getX(),e.getY());
86 }
87 public
88 void mouseExited(MouseEvent e) {
89 mouseStatus("mouse exited at",e.getX(),e.getY());
90 }
91 }
92 );
93
94 }
95
96
97
98
99
100 protected
101 void mouseStatus(String status,
102 int x,
103 int y) {
104 l.setText(status + " " + x + "," + y);
105 }
106}