package de.lmu.tcs; import java.awt.*; /** * View * * Created by jost on 24.11.15. */ public class Ansicht { private final GraphicsWindow fenster; private final int max_x; //Breite private final int max_y; //Höhe private final int skalierung; public Ansicht(int x, int y, int skalierung) { this.max_x = x; this.max_y = y; this.skalierung = skalierung; Point furthestCenter = centerByIndex(x, y); this.fenster = new GraphicsWindow( (int) (furthestCenter.getX() + Math.sqrt(3) * 1/2 * (double) skalierung + (double) (y % 2) * Math.sqrt(3) * 1/2 * (double) skalierung) , (int) (furthestCenter.getY() + (double) skalierung / 2) ); } public void zeichneZelle(Zelle zelle) { Position pos = zelle.getPosition(); // Rectangle box = new Rectangle(pos.getX() * skalierung, pos.getY() * skalierung, skalierung - 1, skalierung - 1); Polygon box = new Hexagon(centerByIndex(pos.getX(), pos.getY()), skalierung).asPolygon(); fenster.setColor(Color.GRAY); fenster.draw(box); if (zelle.istTot()) { fenster.setColor(Param.ZELLENFARBE[0]); } else { fenster.setColor(Param.ZELLENFARBE[Math.min(zelle.alter() + 1, Param.ZELLENFARBE.length - 1)]); } fenster.fill(box); } public void zeichneSpielfeld(Zelle[][] feld) { fenster.clear(); // for (int x = 0; x < max_x; x++) { // for (int y = 0; y < max_y; y++) { // zeichenZelle(feld[x][y]); // } // Äquivalente Alternative ohne explizite Indizes: for (Zelle[] zeile : feld) { for (Zelle zelle : zeile) { if (zelle != null) zeichneZelle(zelle); } } } public Position getClick() { Point point = fenster.mouseClick(); Point firstCenter = centerByIndex(0,0); Position testStart = new Position( (point.x - firstCenter.x) / ((int) (Math.sqrt(3) * (double) skalierung)) , (point.y - firstCenter.y) / ((int) (1.5 * (double) skalierung)) ); // bad guess -- P.S.: actually, now it's a pretty good guess for (int d = 0; d < Math.max(max_x, max_y); d++) // and starting there we test everything (also, another extremely bad guess) for (int dx = -d; dx <= d; dx++) for (int dy = -d; dy <= d; dy++) { if (Math.abs(dx) < d && Math.abs(dy) < d) continue; int x = testStart.getX() + dx; int y = testStart.getY() + dy; // zeichneZelle(new Zelle(new Position(x, y), Zelle.LEBENDIG)); // try {Thread.sleep(100);} catch (Exception e){} Hexagon test = new Hexagon(centerByIndex(x, y), skalierung); if (test.contains(point)) return new Position(x, y); } return new Position(-1, -1); } public void sleep(long delay) { fenster.sleep(delay); } public void setText(String message) { fenster.setText(message); } public Point centerByIndex(int x, int y) { return new Point( (int) (Math.sqrt(3) * ((double) skalierung) * (((double) x) + 1 + ((double) (y % 2)) / 2)) , (int) (((double) skalierung) * (1 + 1.5 * ((double) y))) ); } }