-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallComponent.java
More file actions
41 lines (33 loc) · 863 Bytes
/
BallComponent.java
File metadata and controls
41 lines (33 loc) · 863 Bytes
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
package bounce;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
* The component that draws the balls.
*
* @author Cay Horstmann
* @version 1.34 2012-01-26
*/
public class BallComponent extends JComponent {
public static final int DEFAULT_WIDTH = 450;
public static final int DEFAULT_HEIGHT = 350;
private java.util.List<Ball> balls = new ArrayList<>();
/**
* Add a ball to the component.
*
* @param b the ball to add
*/
public void add(Ball b) {
balls.add(b);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls) {
g2.fill(b.getShape());
}
}
public Dimension getPreferredSize() {
return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}