import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class GameView extends JFrame { private final Board board; private final int hexRadius = 60; private final int hexWidth = (int) (Math.sqrt(3) * hexRadius); // Largeur d'un hexagone private final int hexHeight = 2 * hexRadius; // Hauteur d'un hexagone public GameView(Board board) { this.board = board; setTitle("Dorfromantik - Plateau"); setSize(800, 800); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { handleMouseClick(e.getPoint()); } }); } private void handleMouseClick(Point clickPoint) { // Calcul de la position en coordonnées hexagonales Point hexPosition = calculateHexCoordinates(clickPoint); // Vérifie si la position est libre if (!board.isPositionOccupied(hexPosition)) { Tile newTile = new Tile(Terrain.FORET); // Exemple : tuile de forêt board.addTile(hexPosition, newTile); repaint(); } } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; // Dessiner toutes les tuiles placées for (Point position : board.getTiles().keySet()) { Tile tile = board.getTile(position); int x = calculateScreenX(position); int y = calculateScreenY(position); drawLargeHexagon(g2d, x, y, tile); } } private void drawLargeHexagon(Graphics2D g2d, int x, int y, Tile tile) { Polygon hexagon = new Polygon(); for (int i = 0; i < 6; i++) { double angle = Math.toRadians(60 * i); int px = x + (int) (Math.cos(angle) * hexRadius); int py = y + (int) (Math.sin(angle) * hexRadius); hexagon.addPoint(px, py); } g2d.setColor(getColorForTerrain(tile.getTerrain())); g2d.fillPolygon(hexagon); g2d.setColor(Color.BLACK); g2d.drawPolygon(hexagon); } private Color getColorForTerrain(Terrain terrain) { return switch (terrain) { case MER -> Color.BLUE; case CHAMP -> Color.YELLOW; case FORET -> new Color(34, 139, 34); case PRE -> Color.GREEN; case MONTAGNE -> Color.GRAY; }; } private Point calculateHexCoordinates(Point clickPoint) { int col = (int) Math.round((clickPoint.x - hexWidth / 2.0) / (1.5 * hexRadius)); int row = (int) Math.round((clickPoint.y - hexHeight / 2.0 - (col % 2) * hexHeight / 4.0) / hexHeight); return new Point(col, row); } private int calculateScreenX(Point position) { int col = position.x; return (int) (col * 1.5 * hexRadius + hexWidth / 2.0); } private int calculateScreenY(Point position) { int col = position.x; int row = position.y; return (int) (row * hexHeight + (col % 2) * hexHeight / 2.0 + hexHeight / 2.0); } }