forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhackAMole.java
More file actions
55 lines (47 loc) · 1.35 KB
/
WhackAMole.java
File metadata and controls
55 lines (47 loc) · 1.35 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
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
* Simulates the arcade game of Whack-A-Mole.
* https://en.wikipedia.org/wiki/Whac-A-Mole
*/
public class WhackAMole implements ActionListener {
private Drawing drawing;
private Toolkit toolkit;
/**
* Set up the drawing and window frame.
*/
public WhackAMole() {
// create drawing, add polygons
drawing = new MoleHill(800, 600);
drawing.add(new Mole(150, 200));
drawing.add(new Mole(350, 250));
drawing.add(new Mole(550, 200));
// set up the window frame
JFrame frame = new JFrame("Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(drawing);
frame.pack();
frame.setVisible(true);
toolkit = frame.getToolkit();
}
/**
* Create and start the timer.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
WhackAMole sim = new WhackAMole();
Timer timer = new Timer(1000 / 60, sim);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
drawing.nextact();
drawing.repaint();
toolkit.sync();
}
}