forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoGame.java
50 lines (43 loc) · 1.21 KB
/
VideoGame.java
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
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
/**
* Example game with a sprite that moves around the screen.
*/
public class VideoGame implements ActionListener {
private Drawing drawing;
private Toolkit toolkit;
/**
* Set up the drawing and window frame.
*/
public VideoGame() {
Sprite sprite = new Sprite("face-smile.png", 25, 150);
drawing = new Drawing(800, 600);
drawing.add(sprite);
drawing.addKeyListener(sprite);
drawing.setFocusable(true);
JFrame frame = new JFrame("Video Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(drawing);
frame.pack();
frame.setVisible(true);
toolkit = frame.getToolkit();
}
@Override
public void actionPerformed(ActionEvent e) {
drawing.step();
toolkit.sync();
}
/**
* Create and start the timer.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
VideoGame game = new VideoGame();
Timer timer = new Timer(33, game);
timer.start();
}
}