Début projet avec hexagones

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

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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
View 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);
}
}

View File

@@ -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");
}
});
}
}

View File

@@ -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.

View File

@@ -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.

View File

@@ -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

Width:  |  Height:  |  Size: 124 KiB