✅ Jeu fonctionnel (sauf easter egg)
Co-authored-by: Charpentier Juliette <juliette1.charpentier@etu.u-pec.fr>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.charpentierbalocchi.dorfjavatik.view;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class FenetreDemarrage extends JFrame {
|
||||
|
||||
public FenetreDemarrage() {
|
||||
super("Démarrage du jeu");
|
||||
initUI();
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(400, 200);
|
||||
setLocationRelativeTo(null); // Centre la fenêtre sur l'écran
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
// Création des composants
|
||||
JPanel panelPrincipal = new JPanel(new BorderLayout());
|
||||
JLabel label = new JLabel("Entrez la taille de la grille (1-15):");
|
||||
JTextField textField = new JTextField();
|
||||
textField.setText("5"); // Définir la valeur par défaut à 5
|
||||
JButton button = new JButton("Démarrer la partie");
|
||||
|
||||
// Ajout des composants au panneau principal
|
||||
panelPrincipal.add(label, BorderLayout.NORTH);
|
||||
panelPrincipal.add(textField, BorderLayout.CENTER);
|
||||
panelPrincipal.add(button, BorderLayout.SOUTH);
|
||||
|
||||
// Ajout du panneau principal à la fenêtre
|
||||
add(panelPrincipal);
|
||||
|
||||
// Action listener pour le bouton
|
||||
button.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
try {
|
||||
int taille = Integer.parseInt(textField.getText());
|
||||
if (taille == 1) {
|
||||
// Charger le GIF à partir du chemin de fichier
|
||||
String gifPath = "bin/com/charpentierbalocchi/dorfjavatik/resources/image.gif"; // Remplacer par
|
||||
// le chemin
|
||||
// correct
|
||||
ImageIcon gifIcon = new ImageIcon(gifPath);
|
||||
|
||||
// Vérifier si le GIF est chargé correctement
|
||||
if (gifIcon.getIconWidth() == -1) {
|
||||
JOptionPane.showMessageDialog(null, "Le fichier GIF n'a pas pu être chargé.", "Erreur",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
} else {
|
||||
// Créer un JLabel avec le GIF
|
||||
JLabel gifLabel = new JLabel(gifIcon);
|
||||
JPanel panelMessage = new JPanel(new BorderLayout());
|
||||
panelMessage.add(new JLabel("UNE TUILE!"), BorderLayout.SOUTH);
|
||||
panelMessage.add(gifLabel, BorderLayout.CENTER);
|
||||
|
||||
// Afficher le message avec le GIF dans un JOptionPane
|
||||
JOptionPane.showMessageDialog(null, panelMessage, "UNE TUILE !",
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
} else if (taille >= 2 && taille <= 4) {
|
||||
int response = JOptionPane.showConfirmDialog(null,
|
||||
"Êtes-vous sûr de vouloir continuer avec cette taille de grille ?",
|
||||
"Confirmation",
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
if (response == JOptionPane.YES_OPTION) {
|
||||
new FenetreJeu(taille);
|
||||
dispose();
|
||||
} else {
|
||||
textField.setText(""); // Réinitialiser le champ de texte pour permettre une nouvelle saisie
|
||||
}
|
||||
} else if (taille > 4 && taille <= 15) {
|
||||
new FenetreJeu(taille);
|
||||
dispose();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Veuillez entrer une taille de grille entre 1 et 15.",
|
||||
"Erreur", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Veuillez entrer un nombre valide.", "Erreur",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Méthode principale pour démarrer l'application
|
||||
public static void main(String[] args) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
new FenetreDemarrage();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -7,36 +7,40 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
public class FenetreJeu extends JFrame {
|
||||
private static final int TAILLE_PLATEAU = 5; // Taille du plateau 5x5
|
||||
private int taillePlateau; // Taille du plateau
|
||||
private TileButton[][] boutons; // Boutons représentant les cases du plateau
|
||||
private JLabel labelScore; // Label pour afficher le score
|
||||
private Tuile tuileCourante; // La tuile actuellement proposée
|
||||
private ControleurJeu controleurJeu; // Instance du contrôleur de jeu
|
||||
private JPanel panelPlateau; // Panneau pour le plateau de jeu
|
||||
private JPanel panelTuileCourante; // Panneau pour la tuile courante
|
||||
|
||||
public FenetreJeu(ControleurJeu controleurJeu) {
|
||||
public FenetreJeu(int taillePlateau) {
|
||||
super("DorfJavaTik"); // Titre de la fenêtre
|
||||
this.controleurJeu = controleurJeu;
|
||||
this.taillePlateau = taillePlateau;
|
||||
initUI();
|
||||
this.controleurJeu = new ControleurJeu(this, taillePlateau); // Initialiser le contrôleur après la configuration
|
||||
// de l'interface
|
||||
controleurJeu.demarrerJeu(); // Démarrer le jeu après l'initialisation
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ferme l'application à la fermeture de la fenêtre
|
||||
setSize(1300, 1000); // Fixe la taille de la fenêtre à 1300 x 1000
|
||||
setLocationRelativeTo(null); // Centre la fenêtre sur l'écran
|
||||
pack(); // Ajuste la taille de la fenêtre en fonction des composants
|
||||
}
|
||||
|
||||
public void setControleurJeu(ControleurJeu controleurJeu) {
|
||||
this.controleurJeu = controleurJeu;
|
||||
setVisible(true); // Rendre la fenêtre visible
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
// Panel principal
|
||||
// Panel principal avec BorderLayout
|
||||
JPanel panelPrincipal = new JPanel(new BorderLayout());
|
||||
|
||||
// Panel pour le plateau de jeu
|
||||
JPanel plateau = new JPanel(new GridLayout(TAILLE_PLATEAU, TAILLE_PLATEAU));
|
||||
boutons = new TileButton[TAILLE_PLATEAU][TAILLE_PLATEAU];
|
||||
for (int i = 0; i < TAILLE_PLATEAU; i++) {
|
||||
for (int j = 0; j < TAILLE_PLATEAU; j++) {
|
||||
// Panel pour le plateau de jeu sur la droite
|
||||
panelPlateau = new JPanel(new GridLayout(taillePlateau, taillePlateau));
|
||||
boutons = new TileButton[taillePlateau][taillePlateau];
|
||||
for (int i = 0; i < taillePlateau; i++) {
|
||||
for (int j = 0; j < taillePlateau; j++) {
|
||||
TileButton bouton = new TileButton();
|
||||
final int x = i;
|
||||
final int y = j;
|
||||
@@ -47,16 +51,98 @@ public class FenetreJeu extends JFrame {
|
||||
}
|
||||
});
|
||||
boutons[i][j] = bouton;
|
||||
plateau.add(bouton);
|
||||
panelPlateau.add(bouton);
|
||||
}
|
||||
}
|
||||
|
||||
// Label pour le score
|
||||
// Panel pour afficher la tuile courante et le score sur la gauche, centré
|
||||
// verticalement
|
||||
JPanel panelGauche = new JPanel();
|
||||
panelGauche.setLayout(new BoxLayout(panelGauche, BoxLayout.Y_AXIS));
|
||||
panelGauche.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
||||
|
||||
labelScore = new JLabel("Score: 0");
|
||||
panelPrincipal.add(labelScore, BorderLayout.NORTH);
|
||||
panelPrincipal.add(plateau, BorderLayout.CENTER);
|
||||
labelScore.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
labelScore.setFont(new Font("Serif", Font.BOLD, 24)); // Définir la police plus grande pour le score
|
||||
|
||||
panelTuileCourante = new JPanel() {
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (tuileCourante != null) {
|
||||
int size = Math.min(getWidth(), getHeight());
|
||||
dessinerTuile(g, tuileCourante, size, size);
|
||||
}
|
||||
}
|
||||
};
|
||||
panelTuileCourante.setPreferredSize(new Dimension(150, 150));
|
||||
panelTuileCourante.setMaximumSize(new Dimension(150, 150));
|
||||
panelTuileCourante.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
JButton boutonRecommencer = new JButton("Reset");
|
||||
boutonRecommencer.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
boutonRecommencer.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
controleurJeu.demarrerJeu();
|
||||
}
|
||||
});
|
||||
|
||||
panelGauche.add(Box.createVerticalGlue());
|
||||
panelGauche.add(labelScore);
|
||||
panelGauche.add(Box.createVerticalStrut(20)); // Espace entre les composants
|
||||
panelGauche.add(panelTuileCourante);
|
||||
panelGauche.add(Box.createVerticalStrut(20)); // Espace entre les composants
|
||||
panelGauche.add(boutonRecommencer);
|
||||
panelGauche.add(Box.createVerticalGlue());
|
||||
|
||||
panelPrincipal.add(panelGauche, BorderLayout.WEST);
|
||||
panelPrincipal.add(panelPlateau, BorderLayout.CENTER);
|
||||
|
||||
add(panelPrincipal);
|
||||
|
||||
// Ajouter un écouteur pour garantir que le panneau du plateau reste un carré
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Dimension size = panelPlateau.getSize();
|
||||
int side = Math.min(size.width, size.height);
|
||||
panelPlateau.setPreferredSize(new Dimension(side, side));
|
||||
panelPlateau.revalidate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Méthode pour dessiner les biomes sur un bouton
|
||||
private void dessinerTuile(Graphics g, Tuile tuile, int width, int height) {
|
||||
Graphics2D g2d = (Graphics2D) g.create();
|
||||
int halfWidth = width / 2;
|
||||
int halfHeight = height / 2;
|
||||
|
||||
// Dessiner le triangle nord
|
||||
g2d.setColor(Tuile.getColorForBiome(tuile.getNord()));
|
||||
int[] xPointsNord = { 0, halfWidth, width };
|
||||
int[] yPointsNord = { 0, halfHeight, 0 };
|
||||
g2d.fillPolygon(xPointsNord, yPointsNord, 3);
|
||||
|
||||
// Dessiner le triangle sud
|
||||
g2d.setColor(Tuile.getColorForBiome(tuile.getSud()));
|
||||
int[] xPointsSud = { 0, halfWidth, width };
|
||||
int[] yPointsSud = { height, halfHeight, height };
|
||||
g2d.fillPolygon(xPointsSud, yPointsSud, 3);
|
||||
|
||||
// Dessiner le triangle est
|
||||
g2d.setColor(Tuile.getColorForBiome(tuile.getEst()));
|
||||
int[] xPointsEst = { width, halfWidth, width };
|
||||
int[] yPointsEst = { 0, halfHeight, height };
|
||||
g2d.fillPolygon(xPointsEst, yPointsEst, 3);
|
||||
|
||||
// Dessiner le triangle ouest
|
||||
g2d.setColor(Tuile.getColorForBiome(tuile.getOuest()));
|
||||
int[] xPointsOuest = { 0, halfWidth, 0 };
|
||||
int[] yPointsOuest = { 0, halfHeight, height };
|
||||
g2d.fillPolygon(xPointsOuest, yPointsOuest, 3);
|
||||
|
||||
g2d.dispose();
|
||||
}
|
||||
|
||||
// Méthode pour mettre à jour le plateau après placement d'une tuile
|
||||
@@ -67,6 +153,16 @@ public class FenetreJeu extends JFrame {
|
||||
// position
|
||||
}
|
||||
|
||||
public void resetBoard() {
|
||||
for (int i = 0; i < taillePlateau; i++) {
|
||||
for (int j = 0; j < taillePlateau; j++) {
|
||||
boutons[i][j].setTuile(null); // Réinitialiser la tuile sur le bouton
|
||||
boutons[i][j].setEnabled(true); // Réactiver le bouton
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
labelScore.setText("Score: " + score);
|
||||
}
|
||||
@@ -77,11 +173,16 @@ public class FenetreJeu extends JFrame {
|
||||
|
||||
public void setTuileCourante(Tuile tuile) {
|
||||
this.tuileCourante = tuile;
|
||||
// Affichez la tuile courante dans l'interface utilisateur si nécessaire
|
||||
panelTuileCourante.repaint();
|
||||
}
|
||||
|
||||
public void afficherMessage(String message) {
|
||||
JOptionPane.showMessageDialog(this, message);
|
||||
int result = JOptionPane.showOptionDialog(this, message, "Fin de la partie",
|
||||
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
|
||||
null, new Object[] { "Quitter" }, "Quitter");
|
||||
if (result == JOptionPane.OK_OPTION) {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Classe interne pour représenter un bouton avec une tuile
|
||||
@@ -101,52 +202,9 @@ public class FenetreJeu extends JFrame {
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (tuile != null) {
|
||||
Graphics2D g2d = (Graphics2D) g.create();
|
||||
int width = getWidth();
|
||||
int height = getHeight();
|
||||
|
||||
// Dessiner le triangle nord
|
||||
g2d.setColor(getColorForBiome(tuile.getNord()));
|
||||
int[] xPointsNord = { 0, width / 2, width };
|
||||
int[] yPointsNord = { 0, height / 2, 0 };
|
||||
g2d.fillPolygon(xPointsNord, yPointsNord, 3);
|
||||
|
||||
// Dessiner le triangle sud
|
||||
g2d.setColor(getColorForBiome(tuile.getSud()));
|
||||
int[] xPointsSud = { 0, width / 2, width };
|
||||
int[] yPointsSud = { height, height / 2, height };
|
||||
g2d.fillPolygon(xPointsSud, yPointsSud, 3);
|
||||
|
||||
// Dessiner le triangle est
|
||||
g2d.setColor(getColorForBiome(tuile.getEst()));
|
||||
int[] xPointsEst = { width, width / 2, width };
|
||||
int[] yPointsEst = { 0, height / 2, height };
|
||||
g2d.fillPolygon(xPointsEst, yPointsEst, 3);
|
||||
|
||||
// Dessiner le triangle ouest
|
||||
g2d.setColor(getColorForBiome(tuile.getOuest()));
|
||||
int[] xPointsOuest = { 0, width / 2, 0 };
|
||||
int[] yPointsOuest = { 0, height / 2, height };
|
||||
g2d.fillPolygon(xPointsOuest, yPointsOuest, 3);
|
||||
|
||||
g2d.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private Color getColorForBiome(Tuile.Biome biome) {
|
||||
switch (biome) {
|
||||
case RIVIERE:
|
||||
return Color.BLUE;
|
||||
case FORET:
|
||||
return Color.GREEN;
|
||||
case CHAMP:
|
||||
return Color.YELLOW;
|
||||
case VILLAGE:
|
||||
return Color.RED;
|
||||
case MONTAGNE:
|
||||
return Color.GRAY;
|
||||
default:
|
||||
return Color.BLACK;
|
||||
dessinerTuile(g, tuile, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,11 +213,7 @@ public class FenetreJeu extends JFrame {
|
||||
public static void main(String[] args) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
ControleurJeu controleurJeu = new ControleurJeu(null); // Vous devez initialiser le contrôleur ici avec
|
||||
// une instance de FenetreJeu
|
||||
FenetreJeu fenetreJeu = new FenetreJeu(controleurJeu);
|
||||
controleurJeu.setFenetreJeu(fenetreJeu);
|
||||
fenetreJeu.setVisible(true);
|
||||
new FenetreDemarrage();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user