-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
45 lines (36 loc) · 810 Bytes
/
Ball.java
File metadata and controls
45 lines (36 loc) · 810 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
42
43
44
45
package Chapter14Thread.bounce;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.security.spec.EllipticCurve;
public class Ball {
private static final int XSIZE=15;
private static final int YSIZE=15;
private double x=0;
private double y=0;
private double dx=1;
private double dy=1;
public void move(Rectangle2D bounds){
x+=dx;
y+=dy;
if(x<bounds.getMinX()){
x=bounds.getMinX();
dx=-dx;
}
if(x+XSIZE>=bounds.getMaxX()){
x=bounds.getMaxX()-XSIZE;
dx=-dx;
}
if(y<bounds.getMinY()){
y=bounds.getMinY();
dy=-dy;
}
if(y+YSIZE>=bounds.getMaxY()){
y=bounds.getMaxY()-YSIZE;
dy=-dy;
}
}
public Ellipse2D getShape(){
//在(x,y)处构造长宽为XSIZE和YSIZE的椭圆
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
}
}