diff options
Diffstat (limited to 'ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/Ball.java')
-rw-r--r-- | ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/Ball.java | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/Ball.java b/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/Ball.java new file mode 100644 index 0000000..83628a9 --- /dev/null +++ b/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/Ball.java | |||
@@ -0,0 +1,79 @@ | |||
1 | package de.lmu.ifi.tcs; | ||
2 | |||
3 | import java.awt.Color; | ||
4 | import java.awt.Point; | ||
5 | |||
6 | |||
7 | public class Ball { | ||
8 | |||
9 | private Point randpunkt; | ||
10 | private double pos_x; | ||
11 | private double pos_y; | ||
12 | private double dx; | ||
13 | private double dy; | ||
14 | private long letztesupdate; | ||
15 | private Color farbe = Color.BLACK; | ||
16 | |||
17 | public Ball(Point randpunkt, Point startpunkt, double dx, double dy, Color farbe) { | ||
18 | this.randpunkt = randpunkt; | ||
19 | this.pos_x = startpunkt.getX(); | ||
20 | this.pos_y = startpunkt.getY(); | ||
21 | this.dx = dx; | ||
22 | this.dy = dy; | ||
23 | this.letztesupdate = System.currentTimeMillis(); | ||
24 | this.farbe = farbe; | ||
25 | } | ||
26 | |||
27 | public Ball(Point randpunkt, Point startpunkt, double dx, double dy) { | ||
28 | this(randpunkt, startpunkt, dx, dy, Color.BLACK); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Aktualisiert die Position des Balls gemäß der vestrichenen Zeit. | ||
33 | */ | ||
34 | public void updatePosition() { | ||
35 | long aktuellezeit = System.currentTimeMillis(); | ||
36 | long verstrichen = aktuellezeit - letztesupdate; | ||
37 | pos_x += dx * verstrichen / 1000; | ||
38 | pos_y += dy * verstrichen / 1000; | ||
39 | letztesupdate = aktuellezeit; | ||
40 | if (pos_x<0 || pos_x>randpunkt.x) { | ||
41 | dx *= -1; | ||
42 | pos_x = (2*randpunkt.x - pos_x) % randpunkt.x; | ||
43 | } | ||
44 | if (pos_y<0 || pos_y>randpunkt.y) { | ||
45 | dy *= -1; | ||
46 | pos_y = (2*randpunkt.y - pos_y) % randpunkt.y; | ||
47 | } | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * @return letzte Position des Balles | ||
52 | */ | ||
53 | public Point holePosition() { | ||
54 | return new Point((int) Math.round(pos_x), (int) Math.round(pos_y)); | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * @return aktuelle Farbe des Balls | ||
59 | */ | ||
60 | public Color getFarbe() { | ||
61 | return farbe; | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Wechselt die Farbe des Balls | ||
66 | */ | ||
67 | public void wechsleFarbe() { | ||
68 | if (this.farbe.equals(Color.RED)) { | ||
69 | this.farbe = Color.GREEN; | ||
70 | } else if (this.farbe.equals(Color.GREEN)) { | ||
71 | this.farbe = Color.BLUE; | ||
72 | } else if (this.farbe.equals(Color.BLUE)) { | ||
73 | this.farbe = Color.ORANGE; | ||
74 | } else { | ||
75 | this.farbe = Color.RED; | ||
76 | } | ||
77 | |||
78 | } | ||
79 | } | ||