diff options
Diffstat (limited to 'ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Ansicht.java')
-rw-r--r-- | ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Ansicht.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Ansicht.java b/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Ansicht.java new file mode 100644 index 0000000..c59cd31 --- /dev/null +++ b/ws2015/eip/blaetter/07/H7-1C/de/lmu/tcs/Ansicht.java | |||
@@ -0,0 +1,99 @@ | |||
1 | package de.lmu.tcs; | ||
2 | |||
3 | import java.awt.*; | ||
4 | |||
5 | /** | ||
6 | * View | ||
7 | * | ||
8 | * Created by jost on 24.11.15. | ||
9 | */ | ||
10 | public class Ansicht { | ||
11 | |||
12 | private final GraphicsWindow fenster; | ||
13 | private final int max_x; //Breite | ||
14 | private final int max_y; //Höhe | ||
15 | private final int skalierung; | ||
16 | |||
17 | |||
18 | public Ansicht(int x, int y, int skalierung) { | ||
19 | this.max_x = x; | ||
20 | this.max_y = y; | ||
21 | this.skalierung = skalierung; | ||
22 | Point furthestCenter = centerByIndex(x, y); | ||
23 | this.fenster = new GraphicsWindow( | ||
24 | (int) (furthestCenter.getX() + Math.sqrt(3) * 1/2 * (double) skalierung + (double) (y % 2) * Math.sqrt(3) * 1/2 * (double) skalierung) | ||
25 | , (int) (furthestCenter.getY() + (double) skalierung / 2) | ||
26 | ); | ||
27 | } | ||
28 | |||
29 | public void zeichneZelle(Zelle zelle) { | ||
30 | Position pos = zelle.getPosition(); | ||
31 | // Rectangle box = new Rectangle(pos.getX() * skalierung, pos.getY() * skalierung, skalierung - 1, skalierung - 1); | ||
32 | Polygon box = new Hexagon(centerByIndex(pos.getX(), pos.getY()), skalierung).asPolygon(); | ||
33 | fenster.setColor(Color.GRAY); | ||
34 | fenster.draw(box); | ||
35 | if (zelle.istTot()) { | ||
36 | fenster.setColor(Param.ZELLENFARBE[0]); | ||
37 | } else { | ||
38 | fenster.setColor(Param.ZELLENFARBE[Math.min(zelle.alter() + 1, Param.ZELLENFARBE.length - 1)]); | ||
39 | } | ||
40 | fenster.fill(box); | ||
41 | } | ||
42 | |||
43 | public void zeichneSpielfeld(Zelle[][] feld) { | ||
44 | fenster.clear(); | ||
45 | |||
46 | // for (int x = 0; x < max_x; x++) { | ||
47 | // for (int y = 0; y < max_y; y++) { | ||
48 | // zeichenZelle(feld[x][y]); | ||
49 | // } | ||
50 | // Äquivalente Alternative ohne explizite Indizes: | ||
51 | for (Zelle[] zeile : feld) { | ||
52 | for (Zelle zelle : zeile) { | ||
53 | if (zelle != null) | ||
54 | zeichneZelle(zelle); | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | public Position getClick() { | ||
60 | Point point = fenster.mouseClick(); | ||
61 | Point firstCenter = centerByIndex(0,0); | ||
62 | Position testStart = new Position( | ||
63 | (point.x - firstCenter.x) / ((int) (Math.sqrt(3) * (double) skalierung)) | ||
64 | , (point.y - firstCenter.y) / ((int) (1.5 * (double) skalierung)) | ||
65 | ); // bad guess -- P.S.: actually, now it's a pretty good guess | ||
66 | for (int d = 0; d < Math.max(max_x, max_y); d++) // and starting there we test everything (also, another extremely bad guess) | ||
67 | for (int dx = -d; dx <= d; dx++) | ||
68 | for (int dy = -d; dy <= d; dy++) | ||
69 | { | ||
70 | if (Math.abs(dx) < d && Math.abs(dy) < d) | ||
71 | continue; | ||
72 | int x = testStart.getX() + dx; | ||
73 | int y = testStart.getY() + dy; | ||
74 | // zeichneZelle(new Zelle(new Position(x, y), Zelle.LEBENDIG)); | ||
75 | // try {Thread.sleep(100);} catch (Exception e){} | ||
76 | Hexagon test = new Hexagon(centerByIndex(x, y), skalierung); | ||
77 | if (test.contains(point)) | ||
78 | return new Position(x, y); | ||
79 | } | ||
80 | |||
81 | return new Position(-1, -1); | ||
82 | } | ||
83 | |||
84 | public void sleep(long delay) { | ||
85 | fenster.sleep(delay); | ||
86 | } | ||
87 | |||
88 | public void setText(String message) { | ||
89 | fenster.setText(message); | ||
90 | } | ||
91 | |||
92 | public Point centerByIndex(int x, int y) | ||
93 | { | ||
94 | return new Point( | ||
95 | (int) (Math.sqrt(3) * ((double) skalierung) * (((double) x) + 1 + ((double) (y % 2)) / 2)) | ||
96 | , (int) (((double) skalierung) * (1 + 1.5 * ((double) y))) | ||
97 | ); | ||
98 | } | ||
99 | } | ||