-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPaddleGame.java
More file actions
329 lines (268 loc) · 10.3 KB
/
Copy pathPaddleGame.java
File metadata and controls
329 lines (268 loc) · 10.3 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.tests;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.Timer;
public class PaddleGame extends JFrame implements ActionListener, ChangeListener {
private JTextField vxTextField;
private JTextField vzTextField;
private JLabel vxLabel;
private JLabel vzLabel;
private JLabel sliderLabel;
private JSlider paddleSlider;
private JButton startButton;
private JButton resetButton;
private JPanel drawingPanel;
private GridBagConstraints gbc;
// These fields store ball data and the coefficient
// of restitution.
private double ballVx;
private double ballVz;
private double ballX;
private double ballZ;
private double paddleZ;
private int paddleHeight;
private double ballRadius; // radius of ball
// These elements are used to control the execution
// speed of the game. Without them, the game would
// run too quickly.
private GameUpdater gameUpdater;
private Timer gameTimer;
public PaddleGame() {
// Initialize ball location, velocity, and
// paddle location.
ballVx = 100.0;
ballVz = -71.0;
ballX = 100.0;
ballZ = 100.0;
paddleZ = 100;
paddleHeight = 40;
ballRadius = 5.0;
// Create a Timer object that will be used
// to slow the action down and an ActionListener
// that the Timer will call. The timeDelay variable
// is the time delay in milliseconds.
gameUpdater = new GameUpdater();
int timeDelay = 50;
gameTimer = new Timer(timeDelay, gameUpdater);
// Create JTextField objects to input the initial
// velocity components of the ball.
vxTextField = new JTextField("100.0",6);
vzTextField = new JTextField("-80.0",6);
// Create some JLabels
vxLabel = new JLabel("x-velocity, m/s");
vzLabel = new JLabel("z-velocity, m/s");
sliderLabel = new JLabel("Paddle location");
// Create a JSlider that will move the paddle up and down.
paddleSlider = new JSlider(JSlider.VERTICAL, paddleHeight/2,
200 - paddleHeight/2, (int)paddleZ);
paddleSlider.addChangeListener(this);
// Create a JButton that will start the balls moving
startButton = new JButton("Start");
startButton.setBorder(new BevelBorder(BevelBorder.RAISED));
startButton.setPreferredSize(new Dimension(60,35));
startButton.addActionListener(this);
// Create a JButton that will update the drawing area.
resetButton = new JButton("Reset");
resetButton.setBorder(new BevelBorder(BevelBorder.RAISED));
resetButton.setPreferredSize(new Dimension(60,35));
resetButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// stop the timer.
gameTimer.stop();
// Reset the ball location, velocity, and
// paddle location.
ballVx = 100.0;
ballVz = -80.0;
ballX = 100.0;
ballZ = 100.0;
paddleZ = 100;
paddleSlider.setValue((int)paddleZ);
// Update the display.
updateDisplay();
}
});
// Create a JTextArea that will display the results
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(301, 201));
// Place components on a panel using a GridBagLayout
JPanel northPanel = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
northPanel.setLayout(gridBagLayout1);
int col;
int row;
int numCol = 1;
int numRow = 1;
Insets insets = new Insets(5, 3, 5, 3);
row = 0;
col = 0;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout1.setConstraints(vxLabel, gbc);
col = 1;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout1.setConstraints(vxTextField, gbc);
row = 1;
col = 0;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout1.setConstraints(vzLabel, gbc);
col = 1;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout1.setConstraints(vzTextField, gbc);
row = 0;
col = 2;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout1.setConstraints(startButton, gbc);
col = 3;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout1.setConstraints(resetButton, gbc);
northPanel.add(vxLabel);
northPanel.add(vxTextField);
northPanel.add(vzLabel);
northPanel.add(vzTextField);
northPanel.add(startButton);
northPanel.add(resetButton);
// Place components on a panel using a GridBagLayout
JPanel westPanel = new JPanel();
GridBagLayout gridBagLayout3 = new GridBagLayout();
westPanel.setLayout(gridBagLayout3);
row = 0;
col = 0;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout3.setConstraints(sliderLabel, gbc);
col = 1;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, insets, 0, 0);
gridBagLayout3.setConstraints(paddleSlider, gbc);
westPanel.add(sliderLabel);
westPanel.add(paddleSlider);
// The drawing panel.
JPanel eastPanel = new JPanel();
GridBagLayout gridBagLayout2 = new GridBagLayout();
eastPanel.setLayout(gridBagLayout2);
row = 0;
col = 0;
gbc = new GridBagConstraints(col, row, numCol, numRow,
0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(10, 10, 10, 20), 0, 0);
gridBagLayout2.setConstraints(drawingPanel, gbc);
eastPanel.add(drawingPanel);
// Add the JPanel objects to the content pane
getContentPane().setLayout(new BorderLayout());
getContentPane().add(northPanel, BorderLayout.NORTH);
getContentPane().add(eastPanel, BorderLayout.EAST);
getContentPane().add(westPanel, BorderLayout.WEST);
// Add a title to the JFrame, size it, and make it visible.
setTitle("Paddle Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,500,350);
setVisible(true);
// Update the GUI display
updateDisplay();
}
// The actionPerformed() method is called when
// the "Start" button is pressed.
public void actionPerformed(ActionEvent event) {
// Get the initial quantities from the textfields.
ballVx = Double.parseDouble(vxTextField.getText());
ballVz = Double.parseDouble(vzTextField.getText());
// Update the display
updateDisplay();
// Start the box sliding using a Timer object
// to slow down the action.
gameTimer.start();
}
// The stateChanged() method is called when
// the the JSlider position is changed.
public void stateChanged(ChangeEvent event) {
// Set new paddle location based on position of JSlider.
paddleZ = paddleSlider.getValue();
// Update the display
updateDisplay();
}
// This method redraws the GUI display.
private void updateDisplay() {
Graphics g = drawingPanel.getGraphics();
int width = drawingPanel.getWidth() - 1;
int height = drawingPanel.getHeight() - 1;
g.clearRect(0, 0, width, height);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// Draw outline of game area.
g.setColor(Color.BLACK);
g.drawLine(0, 0, width, 0);
g.drawLine(width, 0, width, height);
g.drawLine(width, height, 0, height);
// Update the position of the ball on the screen.
int xPosition = (int)(ballX - ballRadius);
int zPosition = (int)(height - ballRadius - ballZ);
g.fillOval(xPosition, zPosition, 2*(int)(ballRadius),
2*(int)(ballRadius));
// Update the position of the paddle on the screen.
zPosition = (int)(height - paddleZ);
g.fillRect(10, zPosition - paddleHeight/2, 10, paddleHeight);
}
public static void main(String args[]) {
PaddleGame gui = new PaddleGame();
}
// This ActionListener is called by the Timer
class GameUpdater implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Get dimensions of drawing area.
Graphics g = drawingPanel.getGraphics();
int width = drawingPanel.getWidth() - 1;
int height = drawingPanel.getHeight() - 1;
// Determine if ball collides with right wall.
// If it does, change the x-velocity of the ball.
if ( ballVx > 0.0 && ballX + ballRadius >= width ) {
ballVx = -ballVx;
}
// Determine if ball collides with the top wall.
// If it does, change the z-velocity of the ball.
if ( ballVz > 0.0 && ballZ + ballRadius >= height ) {
ballVz = -ballVz;
}
// Determine if ball collides with the bottom wall.
// If it does, change the z-velocity of the ball.
if ( ballVz < 0.0 && ballZ - ballRadius <= 0.0 ) {
ballVz = -ballVz;
}
// Determine if ball collides with paddle.
// If it does, change the x-velocity of the ball.
if ( ballVx < 0.0 && ballX - ballRadius <= 20.0 ) {
if ( ballZ - ballRadius >= paddleZ - paddleHeight/2 &&
ballZ + ballRadius <= paddleZ + paddleHeight/2 ) {
ballVx = -ballVx;
}
}
// If ball travels off the left edge of the game
// area, stop the simulation.
if ( ballX <= 0.0 ) {
gameTimer.stop();
}
// Compute the new location of the ball.
double timeIncrement = 0.07;
ballX = ballX + timeIncrement*ballVx;
ballZ = ballZ + timeIncrement*ballVz;
// Update the display
updateDisplay();
}
}
}