-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameFrame.java
More file actions
97 lines (84 loc) · 2.71 KB
/
Copy pathGameFrame.java
File metadata and controls
97 lines (84 loc) · 2.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class GameFrame extends JFrame {
private int prints = 0;
private game game = new game();
// starting dimensions of window (pixels)
public static final int WIDTH = 500, HEIGHT = 300, REFRESH = 40;
// where the game objects are displayed
private JPanel panel = new JPanel() {
Image img = Toolkit.getDefaultToolkit().getImage("bg.jpg");
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
game.drawTheGame(g);
// tried to get rid of some stuttering, changing REFRESH
// improved this issue
panel.getToolkit().sync();
}
};
private Timer timer;// timer that runs the game
public GameFrame(String string) {
super(string);
setUpStuff();
}
/**
* Sets up the panel, timer, other initial objects in the game.
* The Timer goes off every REFRESH milliseconds. Every time the
* Timer goes off, the game is told to update itself and then the
* view is refreshed.
*/
private void setUpStuff() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.add(panel);
this.pack();
timer = new Timer(REFRESH, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
game.updateGame();
panel.repaint();
}
});
panel.setBackground(new Color(255, 255, 255));
timer.start();
this.setVisible(true);
panel.requestFocusInWindow();
addKeys(panel);
}
/**
* Proper way to acquire keystrokes in an application.
* This method sets up the mapping which associates a Keystroke (you
* can Google Java KeyStroke API or examples) with an "action command" String
*
* The second part maps the action command String with an Action.
* I have shown you two ways this can be done. You can write the code
* you want executed in the actionPerformed method or you can call the
* method from within the actionPerformed method call.
*/
private void addKeys(JPanel panel) {
// this connects keystroke with a command
panel.getInputMap().put(KeyStroke.getKeyStroke("UP"), "up_key");
panel.getInputMap().put(KeyStroke.getKeyStroke("released UP"), "up_key_r");
panel.getActionMap().put("up_key_r", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("released the up arrow");
game.upReleased(e);
}
});
panel.getActionMap().put("up_key", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("pressed up arrow");
game.upHit(e);
}
});
}
}