diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-19 12:03:25 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-19 12:03:25 +0100 |
commit | 171df5103570946d4e1ff015dee9ed657036e94a (patch) | |
tree | 3638943896562fe767266dfdefbd5a478ed65711 /ws2015/eip/blaetter/10/1/de | |
parent | 0e75e2f631e0cc025248913d426d650fcb722eab (diff) | |
download | uni-171df5103570946d4e1ff015dee9ed657036e94a.tar uni-171df5103570946d4e1ff015dee9ed657036e94a.tar.gz uni-171df5103570946d4e1ff015dee9ed657036e94a.tar.bz2 uni-171df5103570946d4e1ff015dee9ed657036e94a.tar.xz uni-171df5103570946d4e1ff015dee9ed657036e94a.zip |
EiP 10.1
Diffstat (limited to 'ws2015/eip/blaetter/10/1/de')
-rw-r--r-- | ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/HuepfendeBaelle.java | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/HuepfendeBaelle.java b/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/HuepfendeBaelle.java index f6e1eef..a5f1ce8 100644 --- a/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/HuepfendeBaelle.java +++ b/ws2015/eip/blaetter/10/1/de/lmu/ifi/tcs/HuepfendeBaelle.java | |||
@@ -8,8 +8,18 @@ import java.awt.geom.Ellipse2D; | |||
8 | import java.util.ArrayList; | 8 | import java.util.ArrayList; |
9 | import java.util.Random; | 9 | import java.util.Random; |
10 | 10 | ||
11 | /* Änderungen: | ||
12 | * - Runnable interface für HuepfendeBaelle | ||
13 | * - Neue Konstante maxFramerate & update-frequenz limitiert auf maxFramerate updates/sekunde | ||
14 | * - Auslagerung der update & zeichen-schleife in run() | ||
15 | * - Aufrufen von run() in neuem thread | ||
16 | * | ||
17 | * Hinzugefügte Methoden: run | ||
18 | * Veränderte Methoden: main | ||
19 | */ | ||
11 | 20 | ||
12 | public class HuepfendeBaelle { | 21 | |
22 | public class HuepfendeBaelle implements Runnable { | ||
13 | 23 | ||
14 | private ArrayList<Ball> baelle; | 24 | private ArrayList<Ball> baelle; |
15 | private GraphicsWindow gw; | 25 | private GraphicsWindow gw; |
@@ -19,6 +29,9 @@ public class HuepfendeBaelle { | |||
19 | private final static int max_y = 480; | 29 | private final static int max_y = 480; |
20 | private final static int ballgroesse = 20; | 30 | private final static int ballgroesse = 20; |
21 | private final static int knopfgroesse = 40; | 31 | private final static int knopfgroesse = 40; |
32 | |||
33 | private final static int maxFramerate = 30; | ||
34 | |||
22 | /** | 35 | /** |
23 | * @param args | 36 | * @param args |
24 | * @throws InterruptedException | 37 | * @throws InterruptedException |
@@ -41,14 +54,9 @@ public class HuepfendeBaelle { | |||
41 | 54 | ||
42 | HuepfendeBaelle hb = new HuepfendeBaelle(baelle, gw); | 55 | HuepfendeBaelle hb = new HuepfendeBaelle(baelle, gw); |
43 | 56 | ||
57 | Thread draw = new Thread(hb); | ||
58 | draw.start(); | ||
44 | 59 | ||
45 | //while( true ) { | ||
46 | for (Ball ball : baelle) { | ||
47 | ball.updatePosition(); | ||
48 | } | ||
49 | hb.zeichneAlleBaelle(); | ||
50 | //} | ||
51 | |||
52 | // Vearbeitung der Mouse-clicks. | 60 | // Vearbeitung der Mouse-clicks. |
53 | Point click = gw.mouseClick(); | 61 | Point click = gw.mouseClick(); |
54 | Random zufall = new Random(); | 62 | Random zufall = new Random(); |
@@ -57,20 +65,36 @@ public class HuepfendeBaelle { | |||
57 | gw.setText("Treffer! ("+ click.x + "," + click.y +")"); | 65 | gw.setText("Treffer! ("+ click.x + "," + click.y +")"); |
58 | } else { | 66 | } else { |
59 | gw.setText("Daneben! ("+ click.x + "," + click.y +")"); | 67 | gw.setText("Daneben! ("+ click.x + "," + click.y +")"); |
60 | b = new Ball(randpunkt, click, (zufall.nextDouble()-0.5)*200, (zufall.nextDouble()-0.5)*200); | 68 | synchronized (hb) { |
61 | hb.plusBall(b); | 69 | b = new Ball(randpunkt, click, (zufall.nextDouble()-0.5)*200, (zufall.nextDouble()-0.5)*200); |
62 | } | 70 | hb.plusBall(b); |
63 | for (Ball ball : baelle) { | 71 | } |
64 | ball.updatePosition(); | ||
65 | } | 72 | } |
66 | hb.zeichneAlleBaelle(); | 73 | click = gw.mouseClick(); |
67 | click = gw.mouseClick(); // Warten auf Mausklick | ||
68 | } | 74 | } |
69 | gw.setText("Auf Wiedersehen!"); | 75 | |
70 | gw.sleep(8000); | 76 | draw.interrupt(); |
77 | |||
78 | // gw.setText("Auf Wiedersehen!"); | ||
79 | // gw.sleep(8000); | ||
80 | |||
71 | 81 | ||
72 | System.exit(0); | 82 | System.exit(0); |
73 | } | 83 | } |
84 | |||
85 | public void run() { | ||
86 | try { | ||
87 | while(! Thread.interrupted()) { | ||
88 | synchronized (this) { | ||
89 | for (Ball ball : baelle) { | ||
90 | ball.updatePosition(); | ||
91 | } | ||
92 | zeichneAlleBaelle(); | ||
93 | } | ||
94 | Thread.sleep(1000/maxFramerate); | ||
95 | } | ||
96 | } catch (InterruptedException e) {}; | ||
97 | } | ||
74 | 98 | ||
75 | /** | 99 | /** |
76 | * @param baelle | 100 | * @param baelle |