From f2822db0566f39022767d06e91f2b00a8a012230 Mon Sep 17 00:00:00 2001 From: Moncef STITI Date: Tue, 3 Dec 2024 10:14:33 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fr/monkhanny/dorfromantik/Main.java | 2 +- src/fr/monkhanny/dorfromantik/Options.java | 3 + .../game/MainMenuButtonListener.java | 13 +-- .../dorfromantik/game/PauseGame.java | 22 +++-- .../game/SettingsButtonListener.java | 33 ++++++- .../game/SettingsWindowListener.java | 49 +++++++++++ .../dorfromantik/gui/SettingsPanel.java | 87 ++++++++++--------- 7 files changed, 152 insertions(+), 57 deletions(-) create mode 100644 src/fr/monkhanny/dorfromantik/game/SettingsWindowListener.java diff --git a/src/fr/monkhanny/dorfromantik/Main.java b/src/fr/monkhanny/dorfromantik/Main.java index 287276b..9d2812f 100644 --- a/src/fr/monkhanny/dorfromantik/Main.java +++ b/src/fr/monkhanny/dorfromantik/Main.java @@ -67,7 +67,7 @@ public class Main { settingsFrame = new JFrame("Paramètres - Dorfromantik"); howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik"); rewardsFrame = new JFrame("Récompenses - Dorfromantik"); - + // Re-créer et réinitialiser les panels et les contrôleurs MainMenuResizeController mainMenuResizeController = new MainMenuResizeController(Options.mainMenu); MainMenuButtonController mainMenuButtonController = new MainMenuButtonController(Options.mainMenu, settingsFrame, howToPlayFrame, gameModeFrame, gameFrame, rewardsFrame); diff --git a/src/fr/monkhanny/dorfromantik/Options.java b/src/fr/monkhanny/dorfromantik/Options.java index 0e21cfd..ccf5aaa 100644 --- a/src/fr/monkhanny/dorfromantik/Options.java +++ b/src/fr/monkhanny/dorfromantik/Options.java @@ -2,6 +2,7 @@ package fr.monkhanny.dorfromantik; import java.awt.Color; import java.awt.Dimension; +import javax.swing.JFrame; import fr.monkhanny.dorfromantik.gui.MainMenu; public class Options { @@ -46,6 +47,8 @@ public class Options { */ public static int MUSIC_VOLUME = 60; + public static JFrame settingsFrame; + /** * Volume des bruitages */ diff --git a/src/fr/monkhanny/dorfromantik/game/MainMenuButtonListener.java b/src/fr/monkhanny/dorfromantik/game/MainMenuButtonListener.java index 2b64a86..eb95ac6 100644 --- a/src/fr/monkhanny/dorfromantik/game/MainMenuButtonListener.java +++ b/src/fr/monkhanny/dorfromantik/game/MainMenuButtonListener.java @@ -4,24 +4,25 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; -import fr.monkhanny.dorfromantik.Main; import fr.monkhanny.dorfromantik.Options; public class MainMenuButtonListener implements ActionListener { private JFrame gameFrame; private Game game; + private EscapeMenu escapeMenu; - public MainMenuButtonListener(JFrame gameFrame, Game game) { + public MainMenuButtonListener(JFrame gameFrame, Game game, EscapeMenu escapeMenu) { this.gameFrame = gameFrame; this.game = game; + this.escapeMenu = escapeMenu; } @Override public void actionPerformed(ActionEvent e) { - // Réinitialiser le jeu - //Main.resetGame(); - - // ou + // Fermer la fenêtre de pause + if (escapeMenu != null) { + escapeMenu.setVisible(false); + } // Fermer la fenêtre de jeu et revenir au menu principal gameFrame.setVisible(false); diff --git a/src/fr/monkhanny/dorfromantik/game/PauseGame.java b/src/fr/monkhanny/dorfromantik/game/PauseGame.java index 7df42ef..59823e2 100644 --- a/src/fr/monkhanny/dorfromantik/game/PauseGame.java +++ b/src/fr/monkhanny/dorfromantik/game/PauseGame.java @@ -10,28 +10,34 @@ public class PauseGame extends KeyAdapter { private JFrame gameFrame; private Game game; private EscapeMenu escapeMenu; + private ResumeButtonListener resumeButtonListener; + private MainMenuButtonListener mainMenuButtonListener; + private SettingsButtonListener settingsButtonListener; public PauseGame(JFrame gameFrame, Game game) { this.gameFrame = gameFrame; this.game = game; + this.resumeButtonListener = new ResumeButtonListener(gameFrame); + this.mainMenuButtonListener = null; // Initialisé après la création de escapeMenu + this.settingsButtonListener = new SettingsButtonListener(gameFrame); } - + @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { if (Options.isPaused == false) { - // Afficher le menu de pause seulement si aucun menu n'est déjà ouvert Options.isPaused = true; if (escapeMenu == null || !escapeMenu.isVisible()) { escapeMenu = new EscapeMenu(gameFrame, game); - escapeMenu.setResumeButtonListener(new ResumeButtonListener(gameFrame)); - escapeMenu.setMainMenuButtonListener(new MainMenuButtonListener(gameFrame, game)); - escapeMenu.setSettingsButtonListener(new SettingsButtonListener(gameFrame)); + this.mainMenuButtonListener = new MainMenuButtonListener(gameFrame, game, escapeMenu); + escapeMenu.setResumeButtonListener(this.resumeButtonListener); + escapeMenu.setMainMenuButtonListener(this.mainMenuButtonListener); + escapeMenu.setSettingsButtonListener(this.settingsButtonListener); } else { - escapeMenu.setVisible(true); // Si la fenêtre est déjà ouverte, la rendre visible + escapeMenu.setVisible(true); } - escapeMenu.toFront(); // Mettre la fenêtre au premier plan + escapeMenu.toFront(); } } } -} +} \ No newline at end of file diff --git a/src/fr/monkhanny/dorfromantik/game/SettingsButtonListener.java b/src/fr/monkhanny/dorfromantik/game/SettingsButtonListener.java index f3ec70a..398ddec 100644 --- a/src/fr/monkhanny/dorfromantik/game/SettingsButtonListener.java +++ b/src/fr/monkhanny/dorfromantik/game/SettingsButtonListener.java @@ -1,18 +1,47 @@ package fr.monkhanny.dorfromantik.game; +import fr.monkhanny.dorfromantik.Options; +import fr.monkhanny.dorfromantik.gui.SettingsPanel; + import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; public class SettingsButtonListener implements ActionListener { private JFrame gameFrame; + private JFrame settingsFrame; + private SettingsPanel settingsPanel; public SettingsButtonListener(JFrame gameFrame) { this.gameFrame = gameFrame; + this.settingsFrame = new JFrame("Paramètres - Dorfromantik"); + this.settingsPanel = new SettingsPanel(Options.mainMenu, settingsFrame); + this.settingsPanel.setReturnButtonVisible(false); // On cache le bouton de retour au menu principal + + // Ajouter le WindowListener pour réafficher la gameFrame lors de la fermeture de settingsFrame + this.settingsFrame.addWindowListener(new SettingsWindowListener(gameFrame)); } @Override public void actionPerformed(ActionEvent e) { - + // Obtenir la taille et la position de la gameFrame + int width = gameFrame.getWidth(); + int height = gameFrame.getHeight(); + int x = gameFrame.getX(); + int y = gameFrame.getY(); + + // Appliquer ces dimensions et position à settingsFrame + settingsFrame.setSize(width, height); + settingsFrame.setLocation(x, y); + + // Ajouter le panneau des paramètres si ce n'est pas déjà fait + settingsFrame.add(this.settingsPanel); + + // Cacher la gameFrame et afficher settingsFrame + gameFrame.setVisible(false); + settingsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + settingsFrame.setVisible(true); } -} +} \ No newline at end of file diff --git a/src/fr/monkhanny/dorfromantik/game/SettingsWindowListener.java b/src/fr/monkhanny/dorfromantik/game/SettingsWindowListener.java new file mode 100644 index 0000000..4c424d0 --- /dev/null +++ b/src/fr/monkhanny/dorfromantik/game/SettingsWindowListener.java @@ -0,0 +1,49 @@ +package fr.monkhanny.dorfromantik.game; + +import javax.swing.JFrame; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; + +public class SettingsWindowListener implements WindowListener { + private JFrame gameFrame; + + public SettingsWindowListener(JFrame gameFrame) { + this.gameFrame = gameFrame; + } + + @Override + public void windowOpened(WindowEvent e) { + // Rien à faire ici + } + + @Override + public void windowClosing(WindowEvent e) { + // Réafficher la gameFrame + gameFrame.setVisible(true); + } + + @Override + public void windowClosed(WindowEvent e) { + // Rien à faire ici + } + + @Override + public void windowIconified(WindowEvent e) { + // Rien à faire ici + } + + @Override + public void windowDeiconified(WindowEvent e) { + // Rien à faire ici + } + + @Override + public void windowActivated(WindowEvent e) { + // Rien à faire ici + } + + @Override + public void windowDeactivated(WindowEvent e) { + // Rien à faire ici + } +} \ No newline at end of file diff --git a/src/fr/monkhanny/dorfromantik/gui/SettingsPanel.java b/src/fr/monkhanny/dorfromantik/gui/SettingsPanel.java index 21b7310..f85f002 100644 --- a/src/fr/monkhanny/dorfromantik/gui/SettingsPanel.java +++ b/src/fr/monkhanny/dorfromantik/gui/SettingsPanel.java @@ -13,6 +13,7 @@ public class SettingsPanel extends JPanel { private MainMenu mainMenu; private JFrame settingsFrame; + private JButton returnButton; // Déclarer le bouton pour pouvoir y accéder depuis d'autres méthodes public SettingsPanel(MainMenu mainMenu, JFrame settingsFrame) { this.mainMenu = mainMenu; @@ -43,7 +44,7 @@ public class SettingsPanel extends JPanel { title.setHorizontalAlignment(JLabel.CENTER); topPanel.add(title, BorderLayout.CENTER); - JButton returnButton = createReturnButtonWithIcon(); + returnButton = createReturnButtonWithIcon(); // Initialiser le bouton ici JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); leftPanel.setOpaque(false); leftPanel.add(returnButton); @@ -52,6 +53,13 @@ public class SettingsPanel extends JPanel { this.add(topPanel, BorderLayout.NORTH); } + // Méthode pour rendre le bouton de retour visible ou invisible + public void setReturnButtonVisible(boolean visible) { + if (returnButton != null) { + returnButton.setVisible(visible); + } + } + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -144,52 +152,52 @@ public class SettingsPanel extends JPanel { } private JPanel createAutoFocusPanel() { - JPanel panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Disposition verticale - panel.setOpaque(false); // Assurer que le fond est transparent + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Disposition verticale + panel.setOpaque(false); // Assurer que le fond est transparent - // Titre de la section - JLabel titleLabel = new JLabel("Focus Automatique"); - titleLabel.setFont(new Font("Roboto", Font.PLAIN, 30)); - titleLabel.setForeground(Color.WHITE); - titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); // Aligner le texte au centre - panel.add(titleLabel); - panel.add(Box.createVerticalStrut(10)); // Espacement vertical + // Titre de la section + JLabel titleLabel = new JLabel("Focus Automatique"); + titleLabel.setFont(new Font("Roboto", Font.PLAIN, 30)); + titleLabel.setForeground(Color.WHITE); + titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); // Aligner le texte au centre + panel.add(titleLabel); + panel.add(Box.createVerticalStrut(10)); // Espacement vertical - // Panneau contenant texte et case à cocher sur la même ligne - JPanel checkBoxPanel = new JPanel(); - checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.X_AXIS)); // Disposition horizontale - checkBoxPanel.setOpaque(false); // Assurer que le fond est transparent + // Panneau contenant texte et case à cocher sur la même ligne + JPanel checkBoxPanel = new JPanel(); + checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.X_AXIS)); // Disposition horizontale + checkBoxPanel.setOpaque(false); // Assurer que le fond est transparent - // Texte explicatif avant la case à cocher - JLabel descriptionLabel = new JLabel("Gestion du focus automatique (nécessite une bonne carte graphique) :"); - descriptionLabel.setFont(new Font("Roboto", Font.PLAIN, 22)); - descriptionLabel.setForeground(Color.WHITE); - descriptionLabel.setAlignmentX(Component.LEFT_ALIGNMENT); // Aligner à gauche - checkBoxPanel.add(descriptionLabel); // Ajouter le texte dans le panneau + // Texte explicatif avant la case à cocher + JLabel descriptionLabel = new JLabel("Gestion du focus automatique (nécessite une bonne carte graphique) :"); + descriptionLabel.setFont(new Font("Roboto", Font.PLAIN, 22)); + descriptionLabel.setForeground(Color.WHITE); + descriptionLabel.setAlignmentX(Component.LEFT_ALIGNMENT); // Aligner à gauche + checkBoxPanel.add(descriptionLabel); // Ajouter le texte dans le panneau - // Ajouter un espace flexible entre le texte et la case à cocher - checkBoxPanel.add(Box.createHorizontalGlue()); // Cela pousse la case à cocher vers la droite + // Ajouter un espace flexible entre le texte et la case à cocher + checkBoxPanel.add(Box.createHorizontalGlue()); // Cela pousse la case à cocher vers la droite - // Case à cocher - JCheckBox autoFocusCheckBox = new JCheckBox(); - autoFocusCheckBox.setFont(new Font("Roboto", Font.PLAIN, 22)); - autoFocusCheckBox.setFocusPainted(false); - autoFocusCheckBox.setOpaque(false); - autoFocusCheckBox.setBorderPainted(false); - autoFocusCheckBox.setMargin(new Insets(5, 5, 5, 5)); - autoFocusCheckBox.setSelected(Options.AUTO_FOCUS); // État initial selon la valeur actuelle de AUTO_FOCUS - autoFocusCheckBox.addActionListener(e -> { - Options.AUTO_FOCUS = autoFocusCheckBox.isSelected(); // Mettre à jour la variable auto-focus - }); + // Case à cocher + JCheckBox autoFocusCheckBox = new JCheckBox(); + autoFocusCheckBox.setFont(new Font("Roboto", Font.PLAIN, 22)); + autoFocusCheckBox.setFocusPainted(false); + autoFocusCheckBox.setOpaque(false); + autoFocusCheckBox.setBorderPainted(false); + autoFocusCheckBox.setMargin(new Insets(5, 5, 5, 5)); + autoFocusCheckBox.setSelected(Options.AUTO_FOCUS); // État initial selon la valeur actuelle de AUTO_FOCUS + autoFocusCheckBox.addActionListener(e -> { + Options.AUTO_FOCUS = autoFocusCheckBox.isSelected(); // Mettre à jour la variable auto-focus + }); - checkBoxPanel.add(autoFocusCheckBox); // Ajouter la case à cocher + checkBoxPanel.add(autoFocusCheckBox); // Ajouter la case à cocher - // Ajouter le panneau contenant texte + case à cocher - panel.add(checkBoxPanel); + // Ajouter le panneau contenant texte + case à cocher + panel.add(checkBoxPanel); - return panel; -} + return panel; + } private JPanel createSliderPanel(JSlider volumeSlider) { @@ -221,7 +229,6 @@ public class SettingsPanel extends JPanel { return gbc; } - private JButton createReturnButtonWithIcon() { ImageIcon originalIcon = new ImageIcon(Images.EXIT_ICON.getImagePath());