Repository navigation
Expand file tree
/
Copy pathMouseEventDemo
More file actions
75 lines (62 loc) · 2.03 KB
/
Copy pathMouseEventDemo
File metadata and controls
75 lines (62 loc) · 2.03 KB
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
68
69
70
71
72
73
74
75
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseEventDemo9 extends JApplet implements MouseListener
{
private int x; // x coordinate of mouse event
private int y; // y coordinate of mouse event
private String event; // description of mouse event
public void init() // set up GUI
{
setLayout(new FlowLayout());
addMouseListener(this); // listen for mouse events
x = -1; // set x negative for no initial message
}
public void paint(Graphics g) // draw message to screen
{
super.paint(g);
g.drawRect(0, 0, getWidth(), getHeight()); // show bounds of applet
if(x != - 1) // display event during repainting only
{
g.drawString("Mouse event " + event +
" at (" + x + ", " + y + ")",
10, 50);
}
}
public void mousePressed(MouseEvent e) // save coordinates of presses
{
x = e.getX();
y = e.getY();
event = "press";
repaint();
}
public void mouseClicked(MouseEvent e) // save coordinates of clicks
{
x = e.getX();
y = e.getY();
event = "click";
repaint();
}
public void mouseReleased(MouseEvent e) // save coordinates of releases
{
x = e.getX();
y = e.getY();
event = "release";
repaint();
}
public void mouseEntered(MouseEvent e) // save coordinates when mouse enters applet
{
x = e.getX();
y = e.getY();
event = "enter";
repaint();
}
public void mouseExited(MouseEvent e) // save coordinates when mouse leaves applet
{
x = e.getX();
y = e.getY();
event = "exit";
repaint();
}
}
/*<applet code=MouseEventDemo height=300 width=300></applet>*/