-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoGame.java
60 lines (55 loc) · 2.33 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
51
52
53
54
55
56
57
58
59
60
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import java.awt.Toolkit;
//(81)BOOK:A Timer is useful for executing code at regular intervals.
//The constructor for Timer takes two parameters:
// 1. int delay // milliseconds between events
// 2. ActionListener listener // for handling timer events
//The ActionListener interface requires only one method, actionPerformed.
//This is the method the Timer invokes after the given delay. Using a
//Timer, we can reorganize the code in main by defining a class that
//implements ActionListener.
public class VideoGame implements ActionListener
{
//BOOK:The main method constructs a VideoGame object, which creates a
//Sprite, a Drawing, and a JFrame. Then it constructs a Timer object
//and starts the timer. Every 33 milliseconds, the Timer invokes
//actionPerformed, which invokes step on the Drawing. Drawing.step
//invokes step on all of its Actor objects, which causes them to update
//their position, color, or other aspects of their appearance. The
//Drawing.step then repaints the Canvas, and the time step is done.
private Drawing drawing;
public VideoGame()
{
Sprite sprite = new Sprite("C:\\Users\\user\\Desktop\\face-smile.png",
50, 50);
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);
}
//??(91)Well if that is the case then aren't I calling that classes step
//method right here??? since the object invoking step() is a drawing object??
//IDEA:?I believe since we switched the types in Drawing from DP to Actor that
//because Actor is an interface we implemented the draw and step methods from
//Sprite to Actor which is why we used Sprite's methods??
//???BUT drawing has its own draw method too so why isn't it using that???
//??After the repaint() in Drawing the debugger lead me right to this method?Why?
public void actionPerformed(ActionEvent e)
{
drawing.step();
}
public static void main(String[] args)
{
VideoGame game = new VideoGame();
Timer timer = new Timer(33, game);
timer.start();
}
}