-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingObjectsPanel.java
More file actions
120 lines (103 loc) · 3.89 KB
/
MovingObjectsPanel.java
File metadata and controls
120 lines (103 loc) · 3.89 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.Timer;// this timer is best with Swing Components
import javax.swing.JPanel;
public class MovingObjectsPanel extends JPanel {
////here is my code that creates the animation: This code goes in the
////Panel class
private Timer t;// belongs to the class
private final int tickRate = 15;
final Dimension defaultDim;// = new Dimension(800,600);
PlatformerMap gm;
public MovingObjectsPanel(Dimension dim) {
defaultDim = dim;
this.setPreferredSize(defaultDim);
setUpKeyMappings();
makeGameMap();
t.start();// start the timer which starts the "ticking"
}
private void makeGameMap() {
gm = new PlatformerMap();// let the map know what dim is
ActionListener aL = new ActionListener() {// fires off every 10 ms
@Override
public void actionPerformed(ActionEvent arg0) {
gm.tick();// I tell the GameMap to tick... do what
// you do every time the clock goes off.
repaint();// naturally, we want to see the new view
}
};
t = new Timer(tickRate, aL);// this semicolon is here because it is the end of the new Timer construction...
}
public MovingObjectsPanel() {
this( new Dimension(900,1600));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
gm.draw(g);
}
private void setUpKeyMappings() {
// maps keys with actions...
// The code below maps a KeyStroke to an action to be performed
// In this case I mapped the space bar key to the action named "shoot"
// Whenever someone hits the Space Bar the action shoot is sent out
this.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"shoot");
this.getInputMap().put(KeyStroke.getKeyStroke("released W"), "stop");
this.getInputMap().put(KeyStroke.getKeyStroke("released S"), "stop");
this.getInputMap().put(KeyStroke.getKeyStroke("released D"), "stop");
this.getInputMap().put(KeyStroke.getKeyStroke("released A"), "stop");
// This associates the command shoot with some action. In this
// case, the action triggers a shoot command invoked on my GameMap. In general, whatever
// goes in the actionPerformed method will be executed when a shoot command
// is sent...
IsKeyPressed.detectKeyPress();
new Thread(new Runnable() {
@Override
public void run() {
new Timer(60, new ActionListener() {
@Override
public void actionPerformed (ActionEvent actionEvent){
if (IsKeyPressed.isWPressed() || IsKeyPressed.isUpPressed()) {
gm.moveUp();;
}
if (IsKeyPressed.isSPressed() || IsKeyPressed.isDownPressed()) {
gm.moveDown();
}
if (IsKeyPressed.isAPressed() || IsKeyPressed.isLeftPressed()) {
gm.moveLeft();
}
if (IsKeyPressed.isDPressed() || IsKeyPressed.isRightPressed()) {
gm.moveRight();;
}
}
}).start();
}
}).start();
//---------------
this.getActionMap().put("shoot",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
gm.shoot();
}
});
this.getActionMap().put("stop",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
gm.stopDoge();
}
});
this.requestFocusInWindow();
/*
new Timer(750, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gm.shoot();
}
}).start();
*/
}
}