Ajout des règles du jeu + Modification du code

This commit is contained in:
Moncef STITI 2024-04-08 18:16:40 +02:00
parent e612fc48f4
commit 3c7267bca0
8 changed files with 203 additions and 74 deletions

BIN
img/backButton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -0,0 +1,46 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
/**
* Listener for button clicks in the menu.
* It performs different actions based on the button clicked.
*/
class ButtonClickListener implements ActionListener {
private Window window;
/**
* Constructs a ButtonClickListener with the specified window.
* @param window The window where the actions will be performed.
*/
public ButtonClickListener(Window window) {
this.window = window;
}
/**
* Performs an action based on the button clicked.
* @param e The ActionEvent representing the button click.
*/
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = ((Button) e.getSource()).getText();
switch (buttonText) {
case "Jouer":
System.out.println("JOUER"); // À SUPPRIMER APRÈS DEBUG
break;
case "Comment jouer ?":
RulesDialogManager.showRulesDialog(); // Ouvre une fenêtre de dialogue avec les règles
break;
case "Paramètres":
// Code pour les paramètres
break;
case "Quitter":
System.exit(0); // Quitter le programme
break;
default:
break;
}
}
}

View File

@ -1,5 +1,9 @@
import javax.swing.*;
import java.awt.*;
public class Main{ public class Main{
public static void main(String[] args) { public static void main(String[] args) {
MenuView menu = new MenuView(); Window fenetre = new Window(); // Création d'une fenêtre
MenuView menu = new MenuView(fenetre); // Création du menu sur la fenêtre
} }
} }

View File

@ -1,85 +1,75 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
/** public class MenuView {
* A menu view for a Sudoku application.
* @version 1.0
* @author Moncef STITI
* @author Marco ORFAO
*/
public class MenuView extends JFrame { private static final String AUDIO_ON = "../img/iconeAudio.png"; // Chemin vers l'image iconeAudio
private String scheminIconAudio = new String("../img/iconeAudio.png"); private static final String AUDIO_OFF = "../img/iconeAudioMuted.png"; // Chemin vers l'image iconeAudioMuted
private String scheminIconAudioMuted = new String("../img/iconeAudioMuted.png"); private static final String MUSIC_FILE = "../audio/musiqueDeFond.wav"; // Chemin vers la musique de fond
private String scheminMusique = new String("../audio/musiqueDeFond.wav"); private static final Dimension BUTTON_SIZE = new Dimension(300, 60); // Dimension des boutons
private ImageIcon iconAudio = new ImageIcon(scheminIconAudio); private static final Color BACKGROUND_COLOR = new Color(54, 91, 109); // Couleur de l'arrière plan
private ImageIcon iconAudioMuted = new ImageIcon(scheminIconAudioMuted); private static final Color TITLE_TEXT_COLOR = Color.WHITE; // Couleur du titre
private ImageIcon imageSudoku = new ImageIcon("../img/sudoku.png"); // Image Sudoku private static final Font TITLE_FONT = new Font("Copperplate", Font.BOLD, 75); // Police des titres
private Dimension tailleFenetre = new Dimension(1000,700); // Taille de la fenêtre private static final Font SUBTITLE_FONT = new Font("Copperplate", Font.PLAIN, 24); // Police des sous-titres
private Dimension tailleBouton = new Dimension(300, 60); // Taille des boutons private static final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 24); // Police des boutons
private Color couleurFond = new Color(54, 91, 109); // Couleur du fond de la fenêtre private static final String[] BUTTON_TEXTS = {"Jouer", "Comment jouer ?", "Paramètres", "Quitter"}; // Texte des boutons
private Color couleurTexteTitre = Color.WHITE; // Couleur du texte du titre et sous-titre private static final JLabel[] labels = {
private Font policeTitre = new Font("Copperplate", Font.BOLD, 75); // Police du titre new JLabel("Sudoku Solver", SwingConstants.CENTER), // Titre
private Font policeSousTitre = new Font("Copperplate", Font.PLAIN, 24); // Police du sous-titre new JLabel("Par Moncef & Marco", SwingConstants.CENTER)}; // Sous Titre
private Font policeBouton = new Font("Copperplate", Font.BOLD, 24); // Police des boutons
private String[] boutonTextes = {"Jouer", "Comment jouer ?","Paramètres", "Quitter"}; // Textes des boutons
private JLabel[] labels = {new JLabel("Sudoku Solver", SwingConstants.CENTER), // Texte du titre
new JLabel("Par Moncef & Marco", SwingConstants.CENTER)}; // Texte du sous-titre
private final MusicButton musicButton;
private Window window;
public MenuView(Window window) {
this.window = window;
JPanel titlePanel = createTitlePanel();
JPanel buttonPanel = createButtonPanel();
ImageIcon iconeSudoku = new ImageIcon("../img/sudoku.png");
JLabel imageLabel = new JLabel(iconeSudoku);
/** BorderLayout gestionnaireFenetre = new BorderLayout();
* Constructs a MenuView. this.window.getContentPane().setLayout(gestionnaireFenetre);
* Initializes and configures the menu window with buttons and controls. this.window.add(titlePanel, BorderLayout.NORTH);
*/ this.window.add(buttonPanel, BorderLayout.WEST);
public MenuView() { this.window.add(imageLabel, BorderLayout.EAST);
// Création et configuration de la fenêtre this.window.setPageTitle("Menu");
JFrame fenetre = new JFrame("Sudoku - Menu principal");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setMinimumSize(this.tailleFenetre);
fenetre.getContentPane().setBackground(this.couleurFond);
BorderLayout gestionnaireBorderLayout = new BorderLayout();
fenetre.setLayout(gestionnaireBorderLayout);
// Création et ajout du panneau de titre musicButton = new MusicButton(AUDIO_ON, AUDIO_OFF, MUSIC_FILE);
JPanel panneauTitre = new JPanel(new GridLayout(2, 1));
panneauTitre.setBackground(this.couleurFond);
Font[] fonts = {this.policeTitre, this.policeSousTitre};
for (int i = 0; i < this.labels.length; i++) {
this.labels[i].setFont(fonts[i]);
this.labels[i].setForeground(this.couleurTexteTitre);
panneauTitre.add(this.labels[i]);
}
fenetre.add(panneauTitre, BorderLayout.NORTH);
// Création et ajout du panneau de boutons
JPanel panneauBouton = new JPanel();
GridLayout gestionnairePanneauBouton = new GridLayout(boutonTextes.length, 1, 0, 10);
panneauBouton.setLayout(gestionnairePanneauBouton);
panneauBouton.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
panneauBouton.setBackground(this.couleurFond);
Button[] boutons = new Button[boutonTextes.length];
for (int i = 0; i < boutons.length; i++) {
boutons[i] = new Button(this.boutonTextes[i], this.tailleBouton, this.policeBouton, this.couleurFond);
panneauBouton.add(boutons[i]);
}
fenetre.add(panneauBouton, BorderLayout.WEST);
// Ajout de l'image "sudoku.png"
JLabel imageLabel = new JLabel(this.imageSudoku);
fenetre.add(imageLabel, BorderLayout.EAST);
// Ajout du bouton de contrôle de la musique
MusicButton musicButton = new MusicButton(this.scheminIconAudio, this.scheminIconAudioMuted, this.scheminMusique);
FlowLayout gestionnaireControlPanel = new FlowLayout(FlowLayout.RIGHT); FlowLayout gestionnaireControlPanel = new FlowLayout(FlowLayout.RIGHT);
JPanel controlPanel = new JPanel(gestionnaireControlPanel); JPanel controlPanel = new JPanel(gestionnaireControlPanel);
controlPanel.setBackground(this.couleurFond); controlPanel.setBackground(BACKGROUND_COLOR);
controlPanel.add(musicButton); controlPanel.add(musicButton);
fenetre.add(controlPanel, BorderLayout.SOUTH); this.window.add(controlPanel, BorderLayout.SOUTH);
fenetre.pack(); this.window.pack();
fenetre.setLocationRelativeTo(null); this.window.setLocationRelativeTo(null);
fenetre.setVisible(true); this.window.setVisible(true);
}
private JPanel createTitlePanel() {
GridLayout gestionnairePanel = new GridLayout(2, 1);
JPanel panel = new JPanel(gestionnairePanel);
panel.setBackground(BACKGROUND_COLOR);
for (JLabel label : this.labels) {
label.setFont(label == this.labels[0] ? TITLE_FONT : SUBTITLE_FONT);
label.setForeground(TITLE_TEXT_COLOR);
panel.add(label);
}
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
GridLayout layout = new GridLayout(BUTTON_TEXTS.length, 1, 0, 10);
panel.setLayout(layout);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // Permet de ne pas coller les boutons à la fenêtre
panel.setBackground(BACKGROUND_COLOR);
ButtonClickListener listenerButton = new ButtonClickListener(this.window);
for (String text : BUTTON_TEXTS) {
Button button = new Button(text, BUTTON_SIZE, BUTTON_FONT, BACKGROUND_COLOR);
button.addActionListener(listenerButton);
panel.add(button);
}
return panel;
} }
} }

View File

@ -24,7 +24,6 @@ public class MusicButton extends JButton {
* @param offIconPath The file path for the icon when music is off. * @param offIconPath The file path for the icon when music is off.
* @param musicFilePath The file path for the music file to be played. * @param musicFilePath The file path for the music file to be played.
*/ */
public MusicButton(String onIconPath, String offIconPath, String musicFilePath) { public MusicButton(String onIconPath, String offIconPath, String musicFilePath) {
this.iconOn = new ImageIcon(onIconPath); this.iconOn = new ImageIcon(onIconPath);

View File

@ -0,0 +1,8 @@
import javax.swing.JOptionPane;
public class RulesDialogManager {
public static void showRulesDialog() {
RulesSudoku rulesPanel = new RulesSudoku();
JOptionPane.showMessageDialog(null, rulesPanel, "Règles du Sudoku", JOptionPane.PLAIN_MESSAGE);
}
}

35
src/RulesSudoku.java Normal file
View File

@ -0,0 +1,35 @@
import javax.swing.*;
import java.awt.*;
public class RulesSudoku extends JPanel {
public RulesSudoku() {
BorderLayout gestionnaireBorderLayout = new BorderLayout();
this.setLayout(gestionnaireBorderLayout);
this.setBackground(new Color(54, 91, 109)); // Couleur d'arrière-plan du menu principal
JLabel titleLabel = new JLabel("Règles du Sudoku");
titleLabel.setFont(new Font("Copperplate", Font.BOLD, 40)); // Police du titre
titleLabel.setForeground(Color.WHITE); // Couleur du titre
JTextArea rulesTextArea = new JTextArea();
rulesTextArea.setText("Les règles du Sudoku :\n\n" +
"1. Le but du jeu est de remplir la grille avec une série de chiffres de 1 à 9 de telle sorte que chaque ligne, chaque colonne et chaque région de 3x3 contienne tous les chiffres de 1 à 9 sans répétition.\n\n" +
"2. Certains chiffres sont déjà placés dans la grille au départ et ne peuvent pas être modifiés.\n\n" +
"3. Utilisez la logique et le raisonnement pour remplir la grille avec les chiffres manquants.\n\n" +
"4. Le jeu est terminé lorsqu'il n'y a plus de cases vides et que toutes les règles sont respectées.");
rulesTextArea.setEditable(false);
rulesTextArea.setLineWrap(true);
rulesTextArea.setWrapStyleWord(true);
rulesTextArea.setFont(new Font("Arial", Font.PLAIN, 20)); // Police du texte des règles
rulesTextArea.setForeground(Color.WHITE); // Couleur du texte des règles
rulesTextArea.setBackground(new Color(54, 91, 109)); // Couleur d'arrière-plan du texte des règles
JScrollPane scrollPane = new JScrollPane(rulesTextArea);
this.add(titleLabel, BorderLayout.NORTH);
this.add(scrollPane, BorderLayout.CENTER);
this.setPreferredSize(new Dimension(400, 500)); // Taille de la fenêtre des règles
}
}

47
src/Window.java Normal file
View File

@ -0,0 +1,47 @@
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame {
/**
* The minimum size of the window.
*/
private static final Dimension MIN_WINDOW_SIZE = new Dimension(850, 700);
/**
* The title of the program.
*/
private static final String PROGRAM_TITLE = "Sudoku";
/**
* The title of the current page.
*/
private String PAGE_TITLE = "";
/**
* Constructor
*/
public Window() {
super(PROGRAM_TITLE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(this.MIN_WINDOW_SIZE);
this.setLocationRelativeTo(null);
getContentPane().setBackground(new Color(54, 91, 109));
}
/**
* Get the title of the current page.
* @return The title of the current page.
*/
public String getPageTitle() {
return this.PAGE_TITLE;
}
/**
* Set the title of the current page.
* @param title The title of the current page.
*/
public void setPageTitle(String title) {
this.PAGE_TITLE = title;
this.setTitle(this.PAGE_TITLE + " - " + Window.PROGRAM_TITLE);
}
}