test pour afficher les tuiles d'une manière différente

This commit is contained in:
2024-11-16 10:12:24 +01:00
parent dd14eaab2e
commit 22cf8adf3f
9 changed files with 238 additions and 14 deletions

View File

@@ -0,0 +1,12 @@
public class GameController {
private GameView view;
public GameController(GameView view) {
this.view = view;
}
public void startGame() {
System.out.println("Bienvenue dans Dorfromantik simplifié !");
view.showTile();
}
}

62
TestV1/GameView.java Normal file
View File

@@ -0,0 +1,62 @@
import javax.swing.*;
import java.awt.*;
public class GameView extends JFrame {
private Tile tile; // La tuile à afficher
// Constructeur
public GameView(Tile tile) {
this.tile = tile;
setTitle("Dorfromantik - Affichage de tuile");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
// Méthode pour démarrer l'affichage
public void showTile() {
setVisible(true);
repaint(); // Re-dessine la fenêtre
}
@Override
public void paint(Graphics g) {
super.paint(g);
// Récupère la matrice de la tuile
Terrain[][] matrix = tile.getHexMatrix();
// Dessine les terrains sous forme de rectangles
int hexSize = 50; // Taille de chaque "hexagone" simplifiée
int xOffset = 100, yOffset = 100; // Décalage pour centrer
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] != Terrain.VIDE) {
// Définit une couleur en fonction du terrain
g.setColor(getColorForTerrain(matrix[i][j]));
// Dessine un rectangle pour représenter un hexagone (simplifié)
g.fillRect(xOffset + j * hexSize, yOffset + i * hexSize, hexSize, hexSize);
}
}
}
}
// Méthode utilitaire pour associer une couleur à un terrain
private Color getColorForTerrain(Terrain terrain) {
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); // Vert foncé
case MONTAGNE:
return Color.GRAY;
default:
return Color.BLACK;
}
}
}

15
TestV1/Main.java Normal file
View File

@@ -0,0 +1,15 @@
public class Main {
public static void main(String[] args) {
// Exemple : création d'une tuile avec deux terrains
Tile tile = new Tile(Terrain.MER, Terrain.FORET, 1);
// Vue graphique
GameView view = new GameView(tile);
// Contrôleur
GameController controller = new GameController(view);
// Démarrer le jeu
controller.startGame();
}
}

View File

@@ -0,0 +1,11 @@
public class GameController {
private GameView view;
public GameController(GameView view) {
this.view = view;
}
public void startGame() {
view.showTile();
}
}

View File

@@ -0,0 +1,78 @@
import javax.swing.*;
import java.awt.*;
public class GameView extends JFrame {
private Tile tile;
// Constructeur
public GameView(Tile tile) {
this.tile = tile;
setTitle("Dorfromantik - Affichage de tuile");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void showTile() {
setVisible(true);
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
int hexRadius = 50; // Rayon des hexagones
int hexHeight = (int) (Math.sqrt(3) * hexRadius); // Hauteur de l'hexagone
int xOffset = 150, yOffset = 150; // Décalage initial pour centrer
Terrain[][] matrix = tile.getHexMatrix();
// Dessiner les hexagones en fonction de la matrice
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] != Terrain.VIDE) {
// Calcul des coordonnées
int x = xOffset + j * (3 * hexRadius / 2); // Décalage horizontal
int y = yOffset + i * hexHeight + (j % 2) * (hexHeight / 2); // Décalage vertical
drawHexagon(g2d, x, y, hexRadius, getColorForTerrain(matrix[i][j]));
}
}
}
}
private void drawHexagon(Graphics2D g2d, int x, int y, int radius, Color color) {
int[] xPoints = new int[6];
int[] yPoints = new int[6];
for (int i = 0; i < 6; i++) {
xPoints[i] = x + (int) (radius * Math.cos(i * Math.PI / 3));
yPoints[i] = y + (int) (radius * Math.sin(i * Math.PI / 3));
}
g2d.setColor(color);
g2d.fillPolygon(xPoints, yPoints, 6);
g2d.setColor(Color.BLACK); // Contour noir
g2d.drawPolygon(xPoints, yPoints, 6);
}
private Color getColorForTerrain(Terrain terrain) {
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); // Vert foncé
case MONTAGNE:
return Color.GRAY;
default:
return Color.WHITE;
}
}
}

View File

@@ -1,20 +1,15 @@
import model.Game;
import view.GameView;
import controller.GameController;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Game game = new Game();
GameView gameView = new GameView(game); // Crée la vue sans contrôleur pour le moment
GameController controller = new GameController(game, gameView); // Initialise le contrôleur avec game et gameView
// Exemple : une tuile avec deux terrains
Tile tile = new Tile(Terrain.MER, Terrain.FORET, 1);
// Passe ensuite le contrôleur à la vue une fois que tout est initialisé
gameView.setController(controller);
// Vue
GameView view = new GameView(tile);
gameView.setVisible(true); // Affiche la fenêtre
});
// Contrôleur
GameController controller = new GameController(view);
// Démarrer le jeu
controller.startGame();
}
}

View File

@@ -0,0 +1,3 @@
public enum Terrain {
MER, CHAMP, PRE, FORET, MONTAGNE, VIDE
}

View File

@@ -0,0 +1,48 @@
public class Tile {
private Terrain[][] hexMatrix;
// Constructeur pour une tuile avec un seul terrain
public Tile(Terrain terrain) {
hexMatrix = new Terrain[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
hexMatrix[i][j] = terrain;
}
}
}
// Constructeur pour une tuile avec deux terrains
public Tile(Terrain terrain1, Terrain terrain2, int configuration) {
hexMatrix = new Terrain[3][3];
switch (configuration) {
case 1:
fillSides(terrain1, terrain2, 1, 5);
break;
case 2:
fillSides(terrain1, terrain2, 2, 4);
break;
case 3:
fillSides(terrain1, terrain2, 3, 3);
break;
default:
throw new IllegalArgumentException("Configuration invalide");
}
}
private void fillSides(Terrain terrain1, Terrain terrain2, int side1, int side2) {
// Exemple simplifié
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if ((i + j) % 2 == 0) { // Terrain1 sur certaines cases
hexMatrix[i][j] = terrain1;
} else {
hexMatrix[i][j] = terrain2;
}
}
}
}
public Terrain[][] getHexMatrix() {
return hexMatrix;
}
}

BIN
TestV1/dorfromantik.jar Normal file

Binary file not shown.