forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoleHill.java
More file actions
54 lines (46 loc) · 1.15 KB
/
Copy pathMoleHill.java
File metadata and controls
54 lines (46 loc) · 1.15 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
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* Draws a collection of moles and listens for mouse events.
*/
public class MoleHill extends Drawing implements MouseListener {
/**
* Constructs an interactive drawing of given size.
*
* @param width the width in pixels
* @param height the height in pixels
*/
public MoleHill(int width, int height) {
super(width, height);
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
for (Object obj : getActors()) {
if (obj instanceof Mole) {
Mole mole = (Mole) obj;
if (mole.contains(x, y)) {
mole.whack();
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// ignore
}
@Override
public void mouseExited(MouseEvent e) {
// ignore
}
@Override
public void mousePressed(MouseEvent e) {
// ignore
}
@Override
public void mouseReleased(MouseEvent e) {
// ignore
}
}