-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBall.java
More file actions
121 lines (116 loc) · 2.87 KB
/
Copy pathBall.java
File metadata and controls
121 lines (116 loc) · 2.87 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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ball implements Moveable {
private int radius;
private double bx, by;
private double alpha;
private double ball_speed;
public Ball() {
ball_speed = 3;
alpha = 2*Math.PI*Math.random();
setRadius(10);
}
public void InitialPosition(double w, double h, int i) {
bx = w / 2;
by = h / 2;
setRadius((int)(w/80));
}
public void setLocation(int w, int h,double r) {
double deltaR = (w/80)/(r);
if (deltaR != 1){
bx = deltaR*bx;
by = deltaR*by;
setRadius(w/80);
setPlayerSpeed(getSpeed()*deltaR);
}
}
public void move() {
double dx, dy;
dx = Math.cos(alpha);
dy = Math.sin(alpha);
bx = (bx + (ball_speed * dx));
by = (by + (ball_speed * dy));
}
public void drawMe(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
BufferedImage bi = null;
try {
bi = ImageIO.read(new File("ball.jpg"));
} catch (IOException ex) {
System.out.println("File ball.jpg was not found\n");
}
TexturePaint tp = new TexturePaint(bi, new Rectangle((int) bx + 10, (int) by, 50, 50));
g2d.setPaint(tp);
g2d.fillOval((int) bx - radius, (int) by - radius, 2 * radius, 2 * radius);
g2d.setColor(Color.BLACK);
g2d.drawOval((int) bx - radius, (int) by - radius, 2 * radius, 2 * radius);
g2d.dispose();
}
public double getX() {
return bx;
}
public double getY() {
return by;
}
public void setCenter(double x, double y) {
bx = x;
by = y;
}
public double dirX() {
return Math.cos(alpha);
}
public void checkAngle(SoccerField sf) {
double dx, dy;
Rectangle r = sf.getBorders();
dx = Math.cos(alpha);
dy = Math.sin(alpha);
if (dx > 0) { // check right border
if (bx + radius + dx * ball_speed > r.getMaxX())
alpha = Math.PI - alpha;
} else { // check left border
if (bx - radius + dx * ball_speed < r.getMinX())
alpha = Math.PI - alpha;
}
if (dy > 0) { // check bottom border
if (by + radius + dy * ball_speed > r.getMaxY())
alpha = -alpha;
} else { // check top border
if (by - radius + dy * ball_speed < r.getMinY())
alpha = -alpha;
}
}
public void setAlpha(double angle) {
alpha = angle;
}
public double getAlpha() {
return alpha;
}
public int checkGates(int w, int h) {
if (by > (h/2)-(h/7) && by < (h/2)+(h/7)) {
if (bx < (w/22))
return 0;
else if (bx > w - (w/22))
return 1;
}
return -1;
}
private void setPlayerSpeed(double bs) {
ball_speed=bs;
}
private double getSpeed() {
return ball_speed;
}
private void setRadius(int r) {
radius=r;
}
public int getRadius() {
return radius;
}
}