From 8750448693ce08e812d781668d261aed38c1b1f8 Mon Sep 17 00:00:00 2001 From: stiti Date: Tue, 9 Apr 2024 23:28:32 +0200 Subject: [PATCH] Correction de BEAUCOUP de bugs --- src/DialogManager.java | 2 +- src/HomeButtonClickListener.java | 6 +-- src/HomeView.java | 59 ++++++++++++++++++---------- src/Main.java | 3 -- src/MusicButton.java | 25 ++++++------ src/MusicPlayer.java | 2 - src/PlayButtonClickListener.java | 29 ++++++++++++++ src/PlayMenuView.java | 67 +++++++++++++++++++++++++++++++- src/Window.java | 2 +- 9 files changed, 151 insertions(+), 44 deletions(-) create mode 100644 src/PlayButtonClickListener.java diff --git a/src/DialogManager.java b/src/DialogManager.java index 8bae472..317f750 100644 --- a/src/DialogManager.java +++ b/src/DialogManager.java @@ -1,4 +1,4 @@ -import javax.swing.JOptionPane; + public interface DialogManager { void showDialog(); diff --git a/src/HomeButtonClickListener.java b/src/HomeButtonClickListener.java index a7d6691..e09da4f 100644 --- a/src/HomeButtonClickListener.java +++ b/src/HomeButtonClickListener.java @@ -1,7 +1,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.BorderLayout; -import javax.swing.JOptionPane; + /** @@ -32,7 +31,8 @@ class HomeButtonClickListener implements ActionListener { String buttonText = ((Button) e.getSource()).getText(); switch (buttonText) { case "Jouer": - // à faire + PlayMenuView playMenu = new PlayMenuView(window); + window.changeMenu(playMenu); break; case "Règles": rulesDialogManager.showDialog(); diff --git a/src/HomeView.java b/src/HomeView.java index 1e9eb17..d91cc06 100644 --- a/src/HomeView.java +++ b/src/HomeView.java @@ -14,24 +14,32 @@ public class HomeView extends JPanel { 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; + new Title("Sudoku Solver", TITLE_FONT, TITLE_TEXT_COLOR), + new Title("Par Moncef & Marco", SUBTITLE_FONT, TITLE_TEXT_COLOR) + }; + private MusicButton musicButton; + private final Window window; + private JPanel titlePanel; + private JPanel buttonPanel; + private JLabel imageLabel; public HomeView(Window window) { this.window = window; - JPanel titlePanel = new JPanel(); - JPanel buttonPanel = new JPanel(); + createComponents(); + addComponentsToWindow(); + } + + private void createComponents() { + titlePanel = new JPanel(); + buttonPanel = new JPanel(); ImageIcon iconeSudoku = new ImageIcon("img/sudoku.png"); - JLabel imageLabel = new JLabel(iconeSudoku); + 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){ + for (Title label : labels) { titlePanel.add(label); } @@ -47,22 +55,31 @@ public class HomeView extends JPanel { 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"); + musicButton = new MusicButton(AUDIO_ON, AUDIO_OFF, MUSIC_FILE); + } + + public void addComponentsToWindow() { + BorderLayout layout = new BorderLayout(); + window.getContentPane().setLayout(layout); + window.add(titlePanel, BorderLayout.NORTH); + window.add(buttonPanel, BorderLayout.WEST); + window.add(imageLabel, BorderLayout.EAST); + 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); + controlPanel.add(musicButton); + window.add(controlPanel, BorderLayout.SOUTH); - this.window.pack(); - this.window.setLocationRelativeTo(null); - this.window.setVisible(true); + window.pack(); + window.setLocationRelativeTo(null); + window.setVisible(true); + } + + public void removeAllComponents() { + window.getContentPane().removeAll(); + window.revalidate(); + window.repaint(); } } diff --git a/src/Main.java b/src/Main.java index 395862d..cf5d61f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,3 @@ -import javax.swing.*; -import java.awt.*; - public class Main{ public static void main(String[] args) { Window fenetre = new Window(); // Création d'une fenêtre diff --git a/src/MusicButton.java b/src/MusicButton.java index 58b5135..a383176 100644 --- a/src/MusicButton.java +++ b/src/MusicButton.java @@ -1,25 +1,19 @@ -import java.awt.*; import javax.swing.*; /** - * A custom JButton designed to control music playback. * It provides a button that toggles between playing and stopping music when clicked. - * * @version 1.0 * @author Moncef STITI * @author Marco ORFAO */ - -public class MusicButton extends JButton { - private boolean isMusicOn; + public class MusicButton extends JButton { + private static MusicPlayer currentMusicPlayer; private ImageIcon iconOn; private ImageIcon iconOff; private MusicPlayer musicPlayer; - /** * Constructs a MusicButton. - * * @param onIconPath The file path for the icon when music is on. * @param offIconPath The file path for the icon when music is off. * @param musicFilePath The file path for the music file to be played. @@ -29,18 +23,25 @@ public class MusicButton extends JButton { this.iconOn = new ImageIcon(onIconPath); this.iconOff = new ImageIcon(offIconPath); setIcon(this.iconOff); - this.isMusicOn = false; + + // Vérifie s'il y a déjà une musique en cours de lecture et l'arrête si nécessaire + if (currentMusicPlayer != null && currentMusicPlayer.isPlaying()) { + currentMusicPlayer.stop(); + currentMusicPlayer = null; + } + this.musicPlayer = new MusicPlayer(musicFilePath); addActionListener(e -> { - if (this.isMusicOn) { - this.musicPlayer.stop(); + if (currentMusicPlayer != null && currentMusicPlayer.isPlaying()) { + currentMusicPlayer.stop(); + currentMusicPlayer = null; setIcon(this.iconOff); } else { this.musicPlayer.play(); setIcon(this.iconOn); + currentMusicPlayer = this.musicPlayer; } - this.isMusicOn = !this.isMusicOn; }); } } diff --git a/src/MusicPlayer.java b/src/MusicPlayer.java index 963914b..0a451e8 100644 --- a/src/MusicPlayer.java +++ b/src/MusicPlayer.java @@ -14,7 +14,6 @@ public class MusicPlayer { /** * Constructs a MusicPlayer with the specified file path. - * * @param filePath The path to the music file to be played. */ public MusicPlayer(String filePath) { @@ -50,7 +49,6 @@ public class MusicPlayer { /** * Checks if the music is currently playing. - * * @return true if the music is playing, false otherwise. */ public boolean isPlaying() { diff --git a/src/PlayButtonClickListener.java b/src/PlayButtonClickListener.java new file mode 100644 index 0000000..c52b017 --- /dev/null +++ b/src/PlayButtonClickListener.java @@ -0,0 +1,29 @@ +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class PlayButtonClickListener implements ActionListener { + private Window window; + + public PlayButtonClickListener(Window window) { + this.window = window; + } + + @Override + public void actionPerformed(ActionEvent e) { + Button sourceButton = (Button) e.getSource(); + String buttonText = sourceButton.getText(); + + // Handle different button actions based on their text + if (buttonText.equals("Générer une grille")) { + System.out.println("Générer une grille"); + } else if (buttonText.equals("Charger une grille")) { + System.out.println("Chargement de la grille"); + } else if (buttonText.equals("Retour au menu principal")) { + if (window.getContentPane().getComponent(0) instanceof PlayMenuView) { + PlayMenuView playMenuView = (PlayMenuView) window.getContentPane().getComponent(0); + playMenuView.removeAllComponents(window); + HomeView homeView = new HomeView(window); + } + } + } +} diff --git a/src/PlayMenuView.java b/src/PlayMenuView.java index d1e72dd..a75f93d 100644 --- a/src/PlayMenuView.java +++ b/src/PlayMenuView.java @@ -2,5 +2,70 @@ import javax.swing.*; import java.awt.*; public class PlayMenuView extends JPanel { - // à faire + + // View components + 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, 40); + private final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 24); + private final String[] BUTTON_TEXTS = {"Générer une grille", "Charger une grille"}; + private Title titleLabel; + private Button[] playModeButtons; + private Button returnButton; + private JPanel buttonPanel; // Declare buttonPanel here + + // Constructor to initialize components + public PlayMenuView(Window window) { + createComponents(window); + addComponentsToWindow(window); + } + + // Method to create all components + private void createComponents(Window window) { + // Title + titleLabel = new Title("Choix du mode de jeu", TITLE_FONT, TITLE_TEXT_COLOR); + + // Button Panel + GridLayout gestionnaireButtonPanel = new GridLayout(BUTTON_TEXTS.length, 1, 0, 10); + buttonPanel = new JPanel(gestionnaireButtonPanel); // Initialize buttonPanel here + buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); + buttonPanel.setBackground(BACKGROUND_COLOR); + + // Buttons + playModeButtons = new Button[BUTTON_TEXTS.length]; + for (int i = 0; i < BUTTON_TEXTS.length; i++) { + Button button = new Button(BUTTON_TEXTS[i], BUTTON_SIZE, BUTTON_FONT, BACKGROUND_COLOR); + playModeButtons[i] = button; + button.addActionListener(new PlayButtonClickListener(window)); + buttonPanel.add(button); + } + + // Return Button + returnButton = new Button("Retour au menu principal", BUTTON_SIZE, BUTTON_FONT, BACKGROUND_COLOR); + returnButton.addActionListener(new PlayButtonClickListener(window)); + } + + // Method to add components to the window + private void addComponentsToWindow(Window window) { + // Layout + setLayout(new BorderLayout()); + setBackground(BACKGROUND_COLOR); + + // Adding components to the panel + add(titleLabel, BorderLayout.NORTH); + add(buttonPanel, BorderLayout.CENTER); + add(returnButton, BorderLayout.SOUTH); + + + // Add panel to the window + window.add(this); + } + + // Method to remove all components from the window + public void removeAllComponents(Window window) { + window.remove(this); + window.revalidate(); + window.repaint(); + } } diff --git a/src/Window.java b/src/Window.java index ceb0720..21a4cca 100644 --- a/src/Window.java +++ b/src/Window.java @@ -23,7 +23,7 @@ public class Window extends JFrame { public Window() { super(PROGRAM_TITLE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.setMinimumSize(this.MIN_WINDOW_SIZE); + this.setMinimumSize(MIN_WINDOW_SIZE); this.setLocationRelativeTo(null); getContentPane().setBackground(new Color(54, 91, 109)); }