-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
44 lines (40 loc) · 1.08 KB
/
Ball.java
File metadata and controls
44 lines (40 loc) · 1.08 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
package bounce;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
public class Ball {
private static final int X_SIZE = 15;
private static final int Y_SIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1;
/**
* Moves the ball to the next position, reversing direction if it hits one of the edges
*/
public void move(Rectangle2D bounds) {
x += dx;
y += dy;
if (x < bounds.getMinX()) {
x = bounds.getMinX();
dx = -dx;
}
if (x + X_SIZE >= bounds.getMaxX()) {
x = bounds.getMaxX() - X_SIZE;
dx = -dx;
}
if (y < bounds.getMinY()) {
y = bounds.getMinY();
dy = -dy;
}
if (y + Y_SIZE >= bounds.getMaxY()) {
y = bounds.getMaxY() - Y_SIZE;
dy = -dy;
}
}
/**
* Gets the shape of the ball at its current position.
*/
public Ellipse2D getShape() {
return new Ellipse2D.Double(x, y, X_SIZE, Y_SIZE);
}
}