From fa0273f597bd814666c06586e8f6007e33c0d83a Mon Sep 17 00:00:00 2001 From: stiti Date: Tue, 9 Apr 2024 17:13:23 +0200 Subject: [PATCH] Modification du menu + Optimisation --- src/DialogManager.java | 5 ++ ...ener.java => HomeButtonClickListener.java} | 16 ++-- src/HomeView.java | 68 +++++++++++++++++ src/HowToPlayDialogManager.java | 7 +- src/Main.java | 2 +- src/MenuView.java | 76 ------------------- src/PlayMenuView.java | 6 ++ src/RulesDialogManager.java | 7 +- src/Title.java | 11 +++ src/Window.java | 8 ++ 10 files changed, 117 insertions(+), 89 deletions(-) create mode 100644 src/DialogManager.java rename src/{ButtonClickListener.java => HomeButtonClickListener.java} (69%) create mode 100644 src/HomeView.java delete mode 100644 src/MenuView.java create mode 100644 src/PlayMenuView.java create mode 100644 src/Title.java diff --git a/src/DialogManager.java b/src/DialogManager.java new file mode 100644 index 0000000..8bae472 --- /dev/null +++ b/src/DialogManager.java @@ -0,0 +1,5 @@ +import javax.swing.JOptionPane; + +public interface DialogManager { + void showDialog(); +} \ No newline at end of file diff --git a/src/ButtonClickListener.java b/src/HomeButtonClickListener.java similarity index 69% rename from src/ButtonClickListener.java rename to src/HomeButtonClickListener.java index 14db691..a7d6691 100644 --- a/src/ButtonClickListener.java +++ b/src/HomeButtonClickListener.java @@ -3,19 +3,24 @@ 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 { +class HomeButtonClickListener implements ActionListener { private Window window; + private DialogManager rulesDialogManager; + private DialogManager howToPlayDialogManager; /** * Constructs a ButtonClickListener with the specified window. * @param window The window where the actions will be performed. */ - public ButtonClickListener(Window window) { + public HomeButtonClickListener(Window window) { this.window = window; + this.rulesDialogManager = new RulesDialogManager(); + this.howToPlayDialogManager = new HowToPlayDialogManager(); } /** @@ -27,13 +32,13 @@ class ButtonClickListener implements ActionListener { String buttonText = ((Button) e.getSource()).getText(); switch (buttonText) { case "Jouer": - System.out.println("JOUER PRESSER"); // À SUPPRIMER APRÈS DEBUG + // à faire break; case "Règles": - RulesDialogManager.showRulesDialog(); // Ouvre une fenêtre de dialogue avec les règles + rulesDialogManager.showDialog(); break; case "Comment jouer ?": - HowToPlayDialogManager.showHowToPlayDialog(); + howToPlayDialogManager.showDialog(); break; case "Quitter": System.exit(0); // Quitter le programme @@ -42,5 +47,4 @@ class ButtonClickListener implements ActionListener { break; } } - } diff --git a/src/HomeView.java b/src/HomeView.java new file mode 100644 index 0000000..1e9eb17 --- /dev/null +++ b/src/HomeView.java @@ -0,0 +1,68 @@ +import javax.swing.*; +import java.awt.*; + +public class HomeView extends JPanel { + + private final String AUDIO_ON = "img/iconeAudio.png"; + private final String AUDIO_OFF = "img/iconeAudioMuted.png"; + private final String MUSIC_FILE = "audio/musiqueDeFond.wav"; + private final Dimension BUTTON_SIZE = new Dimension(300, 60); + private final Color BACKGROUND_COLOR = new Color(54, 91, 109); + private final Color TITLE_TEXT_COLOR = Color.WHITE; + private final Font TITLE_FONT = new Font("Copperplate", Font.BOLD, 75); + private final Font SUBTITLE_FONT = new Font("Copperplate", Font.PLAIN, 24); + private final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 24); + private final String[] BUTTON_TEXTS = {"Jouer", "Règles", "Comment jouer ?", "Quitter"}; + private final Title[] labels = { + new Title("Sudoku Solver", TITLE_FONT, TITLE_TEXT_COLOR), + new Title("Par Moncef & Marco", SUBTITLE_FONT, TITLE_TEXT_COLOR) + }; + private final MusicButton musicButton; + private Window window; + + public HomeView(Window window) { + this.window = window; + JPanel titlePanel = new JPanel(); + JPanel buttonPanel = new JPanel(); + ImageIcon iconeSudoku = new ImageIcon("img/sudoku.png"); + JLabel imageLabel = new JLabel(iconeSudoku); + + GridLayout titleLayout = new GridLayout(2, 1); + titlePanel.setLayout(titleLayout); + titlePanel.setBackground(BACKGROUND_COLOR); + // Utilisation de la classe Title pour le titre et le sous-titre + for(Title label : labels){ + titlePanel.add(label); + } + + // Button Panel + GridLayout buttonLayout = new GridLayout(BUTTON_TEXTS.length, 1, 0, 10); + buttonPanel.setLayout(buttonLayout); + buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); + buttonPanel.setBackground(BACKGROUND_COLOR); + HomeButtonClickListener listenerButton = new HomeButtonClickListener(window); + for (String text : BUTTON_TEXTS) { + Button button = new Button(text, BUTTON_SIZE, BUTTON_FONT, BACKGROUND_COLOR); + button.addActionListener(listenerButton); + buttonPanel.add(button); + } + + BorderLayout layout = new BorderLayout(); + this.window.getContentPane().setLayout(layout); + this.window.add(titlePanel, BorderLayout.NORTH); + this.window.add(buttonPanel, BorderLayout.WEST); + this.window.add(imageLabel, BorderLayout.EAST); + this.window.setPageTitle("Menu"); + + this.musicButton = new MusicButton(AUDIO_ON, AUDIO_OFF, MUSIC_FILE); + FlowLayout controlPanelLayout = new FlowLayout(FlowLayout.RIGHT); + JPanel controlPanel = new JPanel(controlPanelLayout); + controlPanel.setBackground(BACKGROUND_COLOR); + controlPanel.add(this.musicButton); + this.window.add(controlPanel, BorderLayout.SOUTH); + + this.window.pack(); + this.window.setLocationRelativeTo(null); + this.window.setVisible(true); + } +} diff --git a/src/HowToPlayDialogManager.java b/src/HowToPlayDialogManager.java index 80abb03..c22ff52 100644 --- a/src/HowToPlayDialogManager.java +++ b/src/HowToPlayDialogManager.java @@ -1,8 +1,9 @@ import javax.swing.JOptionPane; -public class HowToPlayDialogManager { - public static void showHowToPlayDialog() { +public class HowToPlayDialogManager implements DialogManager { + @Override + public void showDialog() { HowToPlaySudoku howToPlay = new HowToPlaySudoku(); JOptionPane.showMessageDialog(null, howToPlay, "Comment jouer ?", JOptionPane.PLAIN_MESSAGE); } -} +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 471a58d..395862d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,6 +4,6 @@ import java.awt.*; public class Main{ public static void main(String[] args) { Window fenetre = new Window(); // Création d'une fenêtre - MenuView menu = new MenuView(fenetre); // Création du menu sur la fenêtre + HomeView menu = new HomeView(fenetre); // Création du menu sur la fenêtre } } \ No newline at end of file diff --git a/src/MenuView.java b/src/MenuView.java deleted file mode 100644 index 23f3e73..0000000 --- a/src/MenuView.java +++ /dev/null @@ -1,76 +0,0 @@ -import javax.swing.*; -import java.awt.*; - - -public class MenuView { - - private static final String AUDIO_ON = "img/iconeAudio.png"; // Chemin vers l'image iconeAudio - private static final String AUDIO_OFF = "img/iconeAudioMuted.png"; // Chemin vers l'image iconeAudioMuted - private static final String MUSIC_FILE = "audio/musiqueDeFond.wav"; // Chemin vers la musique de fond - private static final Dimension BUTTON_SIZE = new Dimension(300, 60); // Dimension des boutons - private static final Color BACKGROUND_COLOR = new Color(54, 91, 109); // Couleur de l'arrière plan - private static final Color TITLE_TEXT_COLOR = Color.WHITE; // Couleur du titre - private static final Font TITLE_FONT = new Font("Copperplate", Font.BOLD, 75); // Police des titres - private static final Font SUBTITLE_FONT = new Font("Copperplate", Font.PLAIN, 24); // Police des sous-titres - private static final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 24); // Police des boutons - private static final String[] BUTTON_TEXTS = {"Jouer", "Règles", "Comment jouer ?", "Quitter"}; // Texte des boutons - private static final JLabel[] labels = { - new JLabel("Sudoku Solver", SwingConstants.CENTER), // Titre - new JLabel("Par Moncef & Marco", SwingConstants.CENTER)}; // 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(); - this.window.getContentPane().setLayout(gestionnaireFenetre); - this.window.add(titlePanel, BorderLayout.NORTH); - this.window.add(buttonPanel, BorderLayout.WEST); - this.window.add(imageLabel, BorderLayout.EAST); - this.window.setPageTitle("Menu"); - - musicButton = new MusicButton(AUDIO_ON, AUDIO_OFF, MUSIC_FILE); - FlowLayout gestionnaireControlPanel = new FlowLayout(FlowLayout.RIGHT); - JPanel controlPanel = new JPanel(gestionnaireControlPanel); - controlPanel.setBackground(BACKGROUND_COLOR); - controlPanel.add(musicButton); - this.window.add(controlPanel, BorderLayout.SOUTH); - - this.window.pack(); - this.window.setLocationRelativeTo(null); - 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; - } -} diff --git a/src/PlayMenuView.java b/src/PlayMenuView.java new file mode 100644 index 0000000..d1e72dd --- /dev/null +++ b/src/PlayMenuView.java @@ -0,0 +1,6 @@ +import javax.swing.*; +import java.awt.*; + +public class PlayMenuView extends JPanel { + // à faire +} diff --git a/src/RulesDialogManager.java b/src/RulesDialogManager.java index 260d475..f1c360f 100644 --- a/src/RulesDialogManager.java +++ b/src/RulesDialogManager.java @@ -1,8 +1,9 @@ import javax.swing.JOptionPane; -public class RulesDialogManager { - public static void showRulesDialog() { +public class RulesDialogManager implements DialogManager { + @Override + public void showDialog() { RulesSudoku rulesPanel = new RulesSudoku(); JOptionPane.showMessageDialog(null, rulesPanel, "Règles du Sudoku", JOptionPane.PLAIN_MESSAGE); } -} +} \ No newline at end of file diff --git a/src/Title.java b/src/Title.java new file mode 100644 index 0000000..70ad373 --- /dev/null +++ b/src/Title.java @@ -0,0 +1,11 @@ +import javax.swing.*; +import java.awt.*; + +public class Title extends JLabel { + + public Title(String text, Font font, Color color) { + super(text, SwingConstants.CENTER); + setFont(font); + setForeground(color); + } +} diff --git a/src/Window.java b/src/Window.java index 4b01f09..ceb0720 100644 --- a/src/Window.java +++ b/src/Window.java @@ -44,4 +44,12 @@ public class Window extends JFrame { this.PAGE_TITLE = title; this.setTitle(this.PAGE_TITLE + " - " + Window.PROGRAM_TITLE); } + + public void changeMenu(JPanel menuPanel) { + getContentPane().removeAll(); + getContentPane().add(menuPanel); + revalidate(); + repaint(); + } + }