Début projet avec hexagones

This commit is contained in:
Vincent TEISSIER 2024-10-21 13:21:22 +02:00
parent e70ac8be8a
commit 7b3288542f
26 changed files with 810 additions and 0 deletions

@ -0,0 +1,97 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
import java.util.Random;
public class HexagonGridPattern extends JPanel {
int bigHexRadius = 170;
int smallHexRadius = 30;
Color[] blueShades = {new Color(173, 216, 230), new Color(135, 206, 250), new Color(0, 191, 255)};
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawBigHexagonWithSmallHexagons(g2d, 300, 300, bigHexRadius);
}
public void drawBigHexagonWithSmallHexagons(Graphics2D g2d, int x, int y, int radius) {
g2d.setColor(Color.BLACK);
drawHexagon(g2d, x, y, radius, false);
int xOffset = (int) (smallHexRadius * 2);
int yOffset = (int) (Math.sqrt(3) * smallHexRadius);
Random random = new Random();
for (int row = -3; row <= 3; row++) {
for (int col = -3; col <= 3; col++) {
int xPos = x + col * xOffset;
int yPos = y + row * yOffset;
if (row % 2 != 0) {
xPos += xOffset / 2;
}
if (isInsideBigHexagon(xPos, yPos, x, y, radius)) {
g2d.setColor(blueShades[random.nextInt(blueShades.length)]);
drawInvertedHexagon(g2d, xPos, yPos, smallHexRadius, true);
}
}
}
}
public boolean isInsideBigHexagon(int xPos, int yPos, int centerX, int centerY, int radius) {
double dx = Math.abs(xPos - centerX);
double dy = Math.abs(yPos - centerY);
return dx + dy < radius * Math.sqrt(3);
}
public void drawHexagon(Graphics2D g2d, int x, int y, int radius, boolean fill) {
Path2D hexagon = new Path2D.Double();
for (int i = 0; i < 6; i++) {
double angle = Math.toRadians(60 * i);
int xOffset = (int) (x + radius * Math.cos(angle));
int yOffset = (int) (y + radius * Math.sin(angle));
if (i == 0) {
hexagon.moveTo(xOffset, yOffset);
} else {
hexagon.lineTo(xOffset, yOffset);
}
}
hexagon.closePath();
if (fill) {
g2d.fill(hexagon);
}
g2d.setColor(Color.BLACK);
g2d.draw(hexagon);
}
public void drawInvertedHexagon(Graphics2D g2d, int x, int y, int radius, boolean fill) {
Path2D hexagon = new Path2D.Double();
for (int i = 0; i < 6; i++) {
double angle = Math.toRadians(60 * i + 30);
int xOffset = (int) (x + radius * Math.cos(angle));
int yOffset = (int) (y + radius * Math.sin(angle));
if (i == 0) {
hexagon.moveTo(xOffset, yOffset);
} else {
hexagon.lineTo(xOffset, yOffset);
}
}
hexagon.closePath();
if (fill) {
g2d.fill(hexagon);
}
g2d.setColor(Color.BLACK);
g2d.draw(hexagon);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.add(new HexagonGridPattern());
frame.setVisible(true);
}
}

@ -0,0 +1,72 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
public class HexagonGridWithSmallerHexagons extends JPanel {
int bigHexRadius = 100;
int smallHexRadius = 15;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawBigHexagonWithSmallHexagons(g2d, 200, 200, bigHexRadius);
}
public void drawBigHexagonWithSmallHexagons(Graphics2D g2d, int x, int y, int radius) {
g2d.setColor(Color.BLACK);
drawHexagon(g2d, x, y, radius, true);
int xOffset = (int) (1.5 * smallHexRadius);
int yOffset = (int) (Math.sqrt(3) * smallHexRadius / 2);
for (int row = -3; row <= 3; row++) {
for (int col = -3; col <= 3; col++) {
int xPos = x + col * xOffset;
int yPos = y + row * yOffset * 2;
if (col % 2 != 0) {
yPos += yOffset;
}
if (isInsideBigHexagon(xPos, yPos, x, y, radius)) {
g2d.setColor(new Color(135, 206, 235));
drawHexagon(g2d, xPos, yPos, smallHexRadius, true);
}
}
}
}
public boolean isInsideBigHexagon(int xPos, int yPos, int centerX, int centerY, int radius) {
double dx = Math.abs(xPos - centerX);
double dy = Math.abs(yPos - centerY);
double distance = Math.sqrt(dx * dx + dy * dy);
return distance < radius;
}
public void drawHexagon(Graphics2D g2d, int x, int y, int radius, boolean fill) {
Path2D hexagon = new Path2D.Double();
for (int i = 0; i < 6; i++) {
double angle = Math.toRadians(60 * i);
int xOffset = (int) (x + radius * Math.cos(angle));
int yOffset = (int) (y + radius * Math.sin(angle));
if (i == 0) {
hexagon.moveTo(xOffset, yOffset);
} else {
hexagon.lineTo(xOffset, yOffset);
}
}
hexagon.closePath();
if (fill) {
g2d.fill(hexagon);
}
g2d.setColor(Color.BLACK);
g2d.draw(hexagon);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.add(new HexagonGridWithSmallerHexagons());
frame.setVisible(true);
}
}

@ -0,0 +1,63 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SerieSelection {
public static void main(String[] args) {
JFrame fenetre = new JFrame("Sélection de série");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(600, 200);
fenetre.setLayout(new BorderLayout());
JLabel message = new JLabel("Avec quelle série voulez-vous jouer ?", JLabel.CENTER);
message.setFont(new Font("Arial", Font.BOLD, 16));
fenetre.add(message, BorderLayout.NORTH);
JPanel panelBoutons = new JPanel();
panelBoutons.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
JButton serie1Button = new JButton("Série 1");
JButton serie2Button = new JButton("Série 2");
JButton serie3Button = new JButton("Série 3");
JButton serie4Button = new JButton("Série 4");
serie1Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(fenetre, "Vous avez sélectionné la Série 1 !");
}
});
serie2Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(fenetre, "Vous avez sélectionné la Série 2 !");
}
});
serie3Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(fenetre, "Vous avez sélectionné la Série 3 !");
}
});
serie4Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(fenetre, "Vous avez sélectionné la Série 4 !");
}
});
panelBoutons.add(serie1Button);
panelBoutons.add(serie2Button);
panelBoutons.add(serie3Button);
panelBoutons.add(serie4Button);
fenetre.add(panelBoutons, BorderLayout.CENTER);
fenetre.setVisible(true);
}
}

17
ancien/src/Main.java Normal file

@ -0,0 +1,17 @@
import java.awt.Menu;
import controllers.MenuController;
import models.MenuModel;
import views.MenuView;
public class Main {
public static void main(String[] args) {
// Initialisation du modèle, de la vue et du contrôleur
MenuModel model = new MenuModel();
MenuView view = new MenuView();
MenuController controller = new MenuController(model, view);
// Affichage de la fenêtre
view.setVisible(true);
}
}

@ -0,0 +1,36 @@
package controllers;
import models.MenuModel;
import views.MenuView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuController {
private MenuModel model;
private MenuView view;
public MenuController(MenuModel model, MenuView view) {
this.model = model;
this.view = view;
// Gestion des événements
this.view.addPlayGameListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.setPlayerName(view.getPlayerName());
model.setSelectedSuite(view.getSelectedSuite());
System.out.println("Nom du joueur: " + model.getPlayerName());
System.out.println("Suite sélectionnée: " + model.getSelectedSuite());
}
});
this.view.addContinueGameListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Logique pour continuer la partie
System.out.println("Partie Jouer sélectionnée");
}
});
}
}

@ -0,0 +1,27 @@
package models;
/*public class MenuModel {
private String playerName;
private String selectedSuite;
public MenuModel() {
this.playerName = "";
this.selectedSuite = "";
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getSelectedSuite() {
return selectedSuite;
}
public void setSelectedSuite(String selectedSuite) {
this.selectedSuite = selectedSuite;
} // Exemple mis de coté.
}*/

Binary file not shown.

@ -0,0 +1,12 @@
package views;
import javax.swing.*;
import java.awt.*;
public class BackgroundPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon bg = new ImageIcon("menu.jpg");
g.drawImage(bg.getImage(), 0, 0, getWidth(), getHeight(), this);
}
}

Binary file not shown.

Binary file not shown.

@ -0,0 +1,75 @@
package views;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuView extends JFrame {
private JTextField playerNameField;
private JComboBox<String> suiteSelector;
private JButton playGameButton;
private JButton continueGameButton;
public MenuView() {
// Configuration de la fenêtre
setTitle("Dorf Javatik");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Arrière-plan personnalisé
BackgroundPanel background = new BackgroundPanel();
add(background, BorderLayout.CENTER);
JPanel panelArrondi = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(0, 0, 0, 100));
int arcWidth = 10;
int arcHeight = 10;
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), arcWidth, arcHeight);
}
@Override
public boolean isOpaque() {
return false;
}
};
JButton btn = new JButton("Jouer");
this.playGameButton = btn;
JButton btn2 = new JButton("Continuer");
this.continueGameButton = btn2;
panelArrondi.add(playGameButton);
panelArrondi.add(continueGameButton);
add(panelArrondi, BorderLayout.WEST);
}
public String getPlayerName() {
return playerNameField.getText();
}
public String getSelectedSuite() {
return (String) suiteSelector.getSelectedItem();
}
public void addContinueGameListener(ActionListener listener) {
continueGameButton.addActionListener(listener);
}
public void addPlayGameListener(ActionListener listener) {
playGameButton.addActionListener(listener);
}
}

BIN
ancien/src/views/menu.jpg Normal file

Binary file not shown.

After

(image error) Size: 124 KiB

Binary file not shown.

Binary file not shown.

BIN
bin/model/Tile.class Normal file

Binary file not shown.

BIN
bin/view/GameView$1.class Normal file

Binary file not shown.

BIN
bin/view/GameView$2.class Normal file

Binary file not shown.

BIN
bin/view/GameView.class Normal file

Binary file not shown.

Binary file not shown.

BIN
bin/view/HexagonTile.class Normal file

Binary file not shown.

BIN
bin/view/TileView$1.class Normal file

Binary file not shown.

BIN
bin/view/TileView.class Normal file

Binary file not shown.

@ -0,0 +1,53 @@
package model;
import java.util.Random;
public class Tile {
public enum TerrainType {
MER, CHAMP, PRE, FORET, MONTAGNE
}
private TerrainType[] terrains; // 4 terrains pour chaque quart de la tuile
private static final Random random = new Random();
public Tile() {
this.terrains = new TerrainType[4];
generateTerrains();
}
// Génère des terrains aléatoires pour les 4 parties
private void generateTerrains() {
for (int i = 0; i < 4; i++) {
terrains[i] = generateRandomTerrain();
}
}
// Génère un terrain aléatoire selon certaines probabilités
private TerrainType generateRandomTerrain() {
int rand = random.nextInt(100); // Pourcentage pour chaque terrain
if (rand < 20) {
return TerrainType.MER; // 20% MER
} else if (rand < 40) {
return TerrainType.CHAMP; // 20% CHAMP
} else if (rand < 60) {
return TerrainType.PRE; // 20% PRE
} else if (rand < 80) {
return TerrainType.FORET; // 20% FORET
} else {
return TerrainType.MONTAGNE; // 20% MONTAGNE
}
}
public TerrainType getTerrain(int index) {
if (index >= 0 && index < 4) {
return terrains[index];
}
return null;
}
@Override
public String toString() {
return "Terrains : " + terrains[0] + ", " + terrains[1] + ", " + terrains[2] + ", " + terrains[3];
}
}

@ -0,0 +1,172 @@
package view;
import model.Tile;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class GameView extends JFrame {
private JPanel gridPanel;
private Map<Point, HexagonTile> hexagonMap;
private Set<Point> availablePositions;
private Tile nextTile;
private HexagonTile nextTilePreview; // Tuile de prévisualisation à droite
private int tileCount;
public GameView() {
this.hexagonMap = new HashMap<>();
this.availablePositions = new HashSet<>();
this.tileCount = 0;
setTitle("Jeu de Tuiles");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Générer la première tuile aléatoire
nextTile = generateRandomTile();
// Créer la grille d'hexagones à gauche
gridPanel = createHexagonGrid();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setPreferredSize(new Dimension(600, 600));
add(scrollPane, BorderLayout.CENTER);
// Créer le panneau de contrôle à droite
JPanel controlPanel = createControlPanel();
add(controlPanel, BorderLayout.EAST);
// Placer la première tuile au centre
Point initialPosition = new Point(0, 0);
placeInitialTile(initialPosition);
centerScrollOnPosition(initialPosition, scrollPane);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void placeInitialTile(Point position) {
addHexagonTile(position, gridPanel, 50);
availablePositions.remove(position);
Point[] adjacentPositions = getAdjacentPositions(position);
for (Point adj : adjacentPositions) {
if (!hexagonMap.containsKey(adj)) {
availablePositions.add(adj);
addHexagonTile(adj, gridPanel, 50);
}
}
}
private JPanel createHexagonGrid() {
JPanel panel = new JPanel(null) {
@Override
public Dimension getPreferredSize() {
return new Dimension(3000, 3000);
}
};
return panel;
}
// Ajouter un hexagone à une position donnée
private void addHexagonTile(Point position, JPanel panel, int hexSize) {
int xOffset = position.x * (int) (hexSize * 3 / 2); // Décalage horizontal ajusté
int yOffset = position.y * (int) (Math.sqrt(3) * hexSize); // Décalage vertical ajusté
// Décaler les colonnes impaires verticalement
if (position.x % 2 != 0) {
yOffset += (int) (Math.sqrt(3) * hexSize / 2);
}
HexagonTile hexTile = new HexagonTile(position);
hexTile.setBounds(xOffset, yOffset, hexSize, hexSize);
hexTile.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (availablePositions.contains(hexTile.getPosition())) {
placeTile(hexTile.getPosition());
}
}
});
hexagonMap.put(position, hexTile);
panel.add(hexTile);
panel.revalidate();
panel.repaint();
}
// Placer une tuile à la position spécifiée
private void placeTile(Point position) {
if (availablePositions.contains(position)) {
HexagonTile hexTile = hexagonMap.get(position);
if (hexTile != null && !hexTile.isFilled()) {
// Placer la tuile actuelle
hexTile.setTile(nextTile);
tileCount++;
// Générer une nouvelle tuile et mettre à jour la prévisualisation
nextTile = generateRandomTile();
nextTilePreview.setTile(nextTile);
updateAdjacentPositions(position);
}
}
}
private void updateAdjacentPositions(Point position) {
Point[] adjacentPositions = getAdjacentPositions(position);
for (Point adj : adjacentPositions) {
if (!hexagonMap.containsKey(adj)) {
availablePositions.add(adj);
addHexagonTile(adj, gridPanel, 50);
}
}
}
private Point[] getAdjacentPositions(Point position) {
return new Point[]{
new Point(position.x + 1, position.y),
new Point(position.x - 1, position.y),
new Point(position.x, position.y + 1),
new Point(position.x, position.y - 1),
new Point(position.x + 1, position.y - 1),
new Point(position.x - 1, position.y + 1)
};
}
private Tile generateRandomTile() {
return new Tile();
}
private void centerScrollOnPosition(Point position, JScrollPane scrollPane) {
int xCenter = position.x * 50 * 3 / 2;
int yCenter = position.y * (int) (Math.sqrt(3) * 50);
scrollPane.getViewport().setViewPosition(new Point(xCenter - scrollPane.getViewport().getWidth() / 2,
yCenter - scrollPane.getViewport().getHeight() / 2));
}
private JPanel createControlPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JLabel("Prochaine tuile : "));
panel.add(Box.createRigidArea(new Dimension(0, 10)));
// Afficher la prévisualisation de la prochaine tuile
nextTilePreview = new HexagonTile(null);
nextTilePreview.setPreferredSize(new Dimension(100, 100));
nextTilePreview.setTile(nextTile);
panel.add(nextTilePreview);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GameView());
}
}

@ -0,0 +1,103 @@
package view;
import model.Tile;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
public class HexagonTile extends JPanel {
private Tile tile;
private Point position;
public HexagonTile(Point position) {
this.position = position;
this.tile = null;
setPreferredSize(new Dimension(100, 100)); // Ajuste selon la taille de la tuile
}
public Point getPosition() {
return position;
}
public void setTile(Tile tile) {
this.tile = tile;
repaint();
}
public boolean isFilled() {
return this.tile != null; // Vérifie si la tuile a déjà été placée
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int largeRadius = 50;
// Créer la zone de découpe pour le grand hexagone
Shape largeHexagon = createHexagon(centerX, centerY, largeRadius);
g2d.setClip(largeHexagon);
if (tile != null) {
// Dessiner les 4 quadrants de terrain
drawTerrainQuadrants(g2d, centerX, centerY, largeRadius);
} else {
g2d.setColor(Color.LIGHT_GRAY); // Couleur par défaut pour une case vide
g2d.fill(largeHexagon);
}
// Dessiner la bordure de l'hexagone
g2d.setClip(null);
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3)); // Bordure épaisse
g2d.draw(largeHexagon);
}
// Dessiner les 4 quadrants de terrain
private void drawTerrainQuadrants(Graphics2D g2d, int centerX, int centerY, int radius) {
for (int i = 0; i < 4; i++) {
g2d.setColor(getTerrainColor(tile.getTerrain(i)));
g2d.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, 90 * i, 90);
}
}
// Créer la forme hexagonale
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;
}
// Obtenir la couleur en fonction du type de terrain
private Color getTerrainColor(Tile.TerrainType 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;
}
}
}

@ -0,0 +1,83 @@
package view;
import model.Tile;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
public class TileView extends JPanel {
private Tile tile;
public TileView(Tile tile) {
this.tile = tile;
setPreferredSize(new Dimension(100, 100)); // Ajuste selon la taille de la tuile
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int largeRadius = 50;
// Créer la zone de découpe pour le grand hexagone
Shape largeHexagon = createHexagon(centerX, centerY, largeRadius);
g2d.setClip(largeHexagon);
// Diviser l'hexagone en 4 parties (quart de cercle) pour chaque terrain
drawTerrainQuadrants(g2d, centerX, centerY, largeRadius);
// Dessiner la bordure de l'hexagone
g2d.setClip(null);
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3)); // Bordure épaisse
g2d.draw(largeHexagon);
}
// Dessiner les 4 quadrants de terrain
private void drawTerrainQuadrants(Graphics2D g2d, int centerX, int centerY, int radius) {
for (int i = 0; i < 4; i++) {
g2d.setColor(getTerrainColor(tile.getTerrain(i)));
g2d.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, 90 * i, 90);
}
}
// Créer la forme hexagonale
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;
}
// Obtenir la couleur en fonction du type de terrain
private Color getTerrainColor(Tile.TerrainType 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;
}
}
}