forked from java-tester-x/JavaExercises4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallGame.java
More file actions
126 lines (97 loc) · 3.69 KB
/
BallGame.java
File metadata and controls
126 lines (97 loc) · 3.69 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
package src;
import java.util.Formatter;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class BallGame extends JFrame {
// public static final int CANVAS_WIDTH = 800;
// public static final int CANVAS_HEIGHT = 600;
private String title = "Ball Game";
private int canvasWidth = 800;
private int canvasHeight = 600;
private static final int UPDATE_RATE = 30; // Number of refresh per second
private DrawCanvas canvas;
private Ball ball;
private Ball ball2;
private BallContainer box;
public BallGame() {
canvas = new DrawCanvas(); // Construct a drawing canvas (a JPanel)
canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
// Container cp = getContentPane();
// cp.setLayout(new BorderLayout());
// cp.add(canvas, BorderLayout.CENTER);
setContentPane(canvas);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); // pack all the components in this JFrame
setTitle(title);
setVisible(true); // show this JFrame
initGame();
}
public void initGame()
{
ball = new Ball(50, 50, 10, 5, 30);
ball2 = new Ball(50, 50, 30, 15, 60);
box = new BallContainer(0, 0, canvasWidth, canvasHeight);
// Handling window resize.
canvas.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Component c = (Component)e.getSource();
Dimension dim = c.getSize();
canvasWidth = dim.width;
canvasHeight = dim.height;
// Adjust the bounds of the container to fill the window
box.set(0, 0, canvasWidth, canvasHeight);
}
});
// Start the ball bouncing (in its own thread)
Thread gameThread = new Thread() {
public void run()
{
while (true)
{
box.collidesWith(ball);
ball.move();
box.collidesWith(ball2);
ball2.move();
// Refresh the display
repaint(); // Callback paintComponent()
// Delay for timing control and give other threads a chance
try {
Thread.sleep(1000 / UPDATE_RATE); // milliseconds
} catch (InterruptedException ex) { }
}
}
};
gameThread.start(); // Callback run()
}
/**
* Inner class DrawCanvas (extends JPanel) used for custom graphics drawing.
*/
class DrawCanvas extends JPanel {
@Override
public void paintComponent(Graphics g) { // invoke via repaint()
super.paintComponent(g);
setBackground(Color.LIGHT_GRAY);
g.setColor(Color.WHITE);
ball.draw(g);
ball2.draw(g);
// Graphics2D g2d = (Graphics2D) g;
// g2d.drawString(ball.toString(), ball.getX()+2*ball.getRadius(), ball.getY());
}
@Override
public Dimension getPreferredSize() {
return (new Dimension(canvasWidth, canvasHeight));
}
}
public static void main(String[] args) {
// Run GUI codes in the Event-Dispatching thread for thread safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BallGame(); // Let the constructor do the job
}
});
}
}