Files
SAE31_2024/src/main/java/view/HexagonTile.java

104 lines
3.1 KiB
Java
Raw Normal View History

2024-10-21 13:21:22 +02:00
package view;
import model.Tile;
2024-10-24 20:18:06 +02:00
import model.TerrainType;
2024-10-21 13:21:22 +02:00
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
public class HexagonTile extends JPanel {
private Tile tile;
private Point position;
2024-10-27 16:58:15 +01:00
private boolean isPlaceholder; // Indicateur si l'hexagone est un placeholder
2024-10-21 13:21:22 +02:00
public HexagonTile(Point position, boolean isPlaceholder) {
2024-10-21 13:21:22 +02:00
this.position = position;
this.isPlaceholder = isPlaceholder;
2024-10-21 13:21:22 +02:00
this.tile = null;
setPreferredSize(new Dimension(100, 100));
2024-10-21 13:21:22 +02:00
}
public Point getPosition() {
return position;
}
public void setTile(Tile tile) {
this.tile = tile;
this.isPlaceholder = false; // Une fois la tuile posée, ce n'est plus un placeholder
2024-10-21 13:21:22 +02:00
repaint();
}
public Tile getTile() {
return tile;
}
2024-10-21 13:21:22 +02:00
public boolean isFilled() {
return this.tile != null;
2024-10-21 13:21:22 +02:00
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int largeRadius = isPlaceholder ? 40 : 50; // Réduction de taille pour les placeholders
2024-10-21 13:21:22 +02:00
Shape largeHexagon = createHexagon(centerX, centerY, largeRadius);
g2d.setClip(largeHexagon);
if (tile != null) {
2024-10-27 16:58:15 +01:00
drawTerrainSegments(g2d, centerX, centerY, largeRadius);
2024-10-21 13:21:22 +02:00
} else {
g2d.setColor(Color.LIGHT_GRAY);
2024-10-21 13:21:22 +02:00
g2d.fill(largeHexagon);
}
g2d.setClip(null);
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3));
2024-10-21 13:21:22 +02:00
g2d.draw(largeHexagon);
}
2024-10-27 16:58:15 +01:00
private void drawTerrainSegments(Graphics2D g2d, int centerX, int centerY, int radius) {
// Parcourt les segments de 0 à 5 pour dessiner chaque segment en fonction du terrain associé
2024-10-25 00:59:58 +02:00
for (int i = 0; i < 6; i++) {
2024-10-27 16:58:15 +01:00
TerrainType terrain = tile.getTerrainForSegment(i); // Récupère le terrain du segment, en prenant en compte la rotation
g2d.setColor(getTerrainColor(terrain));
2024-10-25 00:59:58 +02:00
g2d.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, 60 * i, 60);
2024-10-21 13:21:22 +02:00
}
}
private Shape createHexagon(int centerX, int centerY, int radius) {
Path2D hexagon = new Path2D.Double();
for (int i = 0; i < 6; i++) {
double angle = Math.toRadians(60 * i);
double x = centerX + radius * Math.cos(angle);
double y = centerY + radius * Math.sin(angle);
if (i == 0) {
hexagon.moveTo(x, y);
} else {
hexagon.lineTo(x, y);
}
}
hexagon.closePath();
return hexagon;
}
2024-10-24 17:59:46 +02:00
private Color getTerrainColor(TerrainType terrain) {
2024-10-25 00:59:58 +02:00
if (terrain == null) {
return Color.WHITE;
2024-10-25 00:59:58 +02:00
}
2024-10-21 13:21:22 +02:00
switch (terrain) {
case MER: return Color.BLUE;
case CHAMP: return Color.YELLOW;
case PRE: return Color.GREEN;
case FORET: return new Color(34, 139, 34);
case MONTAGNE: return Color.GRAY;
default: return Color.WHITE;
2024-10-21 13:21:22 +02:00
}
}
}