-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
94 lines (69 loc) · 1.62 KB
/
Ball.java
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
package pongy;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
public class Ball {
GeneralPath ball;
PlayField pField;
int ballRadius;
double xCoord, yCoord;
int angle;
double velocity;
public Ball() {
ball = new GeneralPath();
}
public int getX() {
return ( int )xCoord;
}
public int getY() {
return ( int )yCoord;
}
public int getRadius() {
return ballRadius;
}
public int getAngle() {
return angle;
}
public double getVelocity() {
return velocity;
}
public void setX( int x ) {
xCoord = ( double )x;
}
public void setY( int y ) {
yCoord = ( double )y;
}
public void setRadius( int r ) {
ballRadius = r;
}
public void setField( PlayField f ) {
pField = f;
}
public void setAngle( int d ) {
angle = d;
}
public void setVelocity( double v ) {
velocity = v;
}
private void calculateCoords() {
if ( (xCoord-( double )ballRadius) < pField.getLeftBound() ) {
xCoord = pField.getLeftBound()+( double )ballRadius;
angle = (540-angle)%360;
}
if ( (xCoord+( double )ballRadius) > pField.getRightBound() ) {
xCoord = pField.getRightBound()-( double )ballRadius;
angle = (540-angle)%360;
}
xCoord += velocity*Math.cos(angle*Math.PI/180);
yCoord -= velocity*Math.sin(angle*Math.PI/180);
}
public Shape getBall() {
calculateCoords();
ball.reset();
ball.moveTo( xCoord+ballRadius*Math.cos( 0 ), yCoord+ballRadius*Math.sin( 0 ) );
for ( int i = 1; i <= 12; i++ ) {
ball.lineTo( xCoord+Math.round( ballRadius*Math.cos( Math.PI*i/6 ) ), yCoord+Math.round( ballRadius*Math.sin( Math.PI*i/6 ) ) );
}
ball.closePath();
return ( Shape )ball;
}
}