Correction de BEAUCOUP de bugs

This commit is contained in:
Moncef STITI 2024-04-09 23:28:32 +02:00
parent fa0273f597
commit 8750448693
9 changed files with 151 additions and 44 deletions

View File

@ -1,4 +1,4 @@
import javax.swing.JOptionPane;
public interface DialogManager { public interface DialogManager {
void showDialog(); void showDialog();

View File

@ -1,7 +1,6 @@
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 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(); String buttonText = ((Button) e.getSource()).getText();
switch (buttonText) { switch (buttonText) {
case "Jouer": case "Jouer":
// à faire PlayMenuView playMenu = new PlayMenuView(window);
window.changeMenu(playMenu);
break; break;
case "Règles": case "Règles":
rulesDialogManager.showDialog(); rulesDialogManager.showDialog();

View File

@ -14,24 +14,32 @@ public class HomeView extends JPanel {
private final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 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 String[] BUTTON_TEXTS = {"Jouer", "Règles", "Comment jouer ?", "Quitter"};
private final Title[] labels = { private final Title[] labels = {
new Title("Sudoku Solver", TITLE_FONT, TITLE_TEXT_COLOR), new Title("Sudoku Solver", TITLE_FONT, TITLE_TEXT_COLOR),
new Title("Par Moncef & Marco", SUBTITLE_FONT, TITLE_TEXT_COLOR) new Title("Par Moncef & Marco", SUBTITLE_FONT, TITLE_TEXT_COLOR)
}; };
private final MusicButton musicButton; private MusicButton musicButton;
private Window window; private final Window window;
private JPanel titlePanel;
private JPanel buttonPanel;
private JLabel imageLabel;
public HomeView(Window window) { public HomeView(Window window) {
this.window = window; this.window = window;
JPanel titlePanel = new JPanel(); createComponents();
JPanel buttonPanel = new JPanel(); addComponentsToWindow();
}
private void createComponents() {
titlePanel = new JPanel();
buttonPanel = new JPanel();
ImageIcon iconeSudoku = new ImageIcon("img/sudoku.png"); ImageIcon iconeSudoku = new ImageIcon("img/sudoku.png");
JLabel imageLabel = new JLabel(iconeSudoku); imageLabel = new JLabel(iconeSudoku);
GridLayout titleLayout = new GridLayout(2, 1); GridLayout titleLayout = new GridLayout(2, 1);
titlePanel.setLayout(titleLayout); titlePanel.setLayout(titleLayout);
titlePanel.setBackground(BACKGROUND_COLOR); titlePanel.setBackground(BACKGROUND_COLOR);
// Utilisation de la classe Title pour le titre et le sous-titre // Utilisation de la classe Title pour le titre et le sous-titre
for(Title label : labels){ for (Title label : labels) {
titlePanel.add(label); titlePanel.add(label);
} }
@ -47,22 +55,31 @@ public class HomeView extends JPanel {
buttonPanel.add(button); buttonPanel.add(button);
} }
BorderLayout layout = new BorderLayout(); musicButton = new MusicButton(AUDIO_ON, AUDIO_OFF, MUSIC_FILE);
this.window.getContentPane().setLayout(layout); }
this.window.add(titlePanel, BorderLayout.NORTH);
this.window.add(buttonPanel, BorderLayout.WEST); public void addComponentsToWindow() {
this.window.add(imageLabel, BorderLayout.EAST); BorderLayout layout = new BorderLayout();
this.window.setPageTitle("Menu"); 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); FlowLayout controlPanelLayout = new FlowLayout(FlowLayout.RIGHT);
JPanel controlPanel = new JPanel(controlPanelLayout); JPanel controlPanel = new JPanel(controlPanelLayout);
controlPanel.setBackground(BACKGROUND_COLOR); controlPanel.setBackground(BACKGROUND_COLOR);
controlPanel.add(this.musicButton); controlPanel.add(musicButton);
this.window.add(controlPanel, BorderLayout.SOUTH); window.add(controlPanel, BorderLayout.SOUTH);
this.window.pack(); window.pack();
this.window.setLocationRelativeTo(null); window.setLocationRelativeTo(null);
this.window.setVisible(true); window.setVisible(true);
}
public void removeAllComponents() {
window.getContentPane().removeAll();
window.revalidate();
window.repaint();
} }
} }

View File

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

View File

@ -1,25 +1,19 @@
import java.awt.*;
import javax.swing.*; import javax.swing.*;
/** /**
* A custom JButton designed to control music playback.
* It provides a button that toggles between playing and stopping music when clicked. * It provides a button that toggles between playing and stopping music when clicked.
*
* @version 1.0 * @version 1.0
* @author Moncef STITI * @author Moncef STITI
* @author Marco ORFAO * @author Marco ORFAO
*/ */
public class MusicButton extends JButton {
public class MusicButton extends JButton { private static MusicPlayer currentMusicPlayer;
private boolean isMusicOn;
private ImageIcon iconOn; private ImageIcon iconOn;
private ImageIcon iconOff; private ImageIcon iconOff;
private MusicPlayer musicPlayer; private MusicPlayer musicPlayer;
/** /**
* Constructs a MusicButton. * Constructs a MusicButton.
*
* @param onIconPath The file path for the icon when music is on. * @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 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.
@ -29,18 +23,25 @@ public class MusicButton extends JButton {
this.iconOn = new ImageIcon(onIconPath); this.iconOn = new ImageIcon(onIconPath);
this.iconOff = new ImageIcon(offIconPath); this.iconOff = new ImageIcon(offIconPath);
setIcon(this.iconOff); 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); this.musicPlayer = new MusicPlayer(musicFilePath);
addActionListener(e -> { addActionListener(e -> {
if (this.isMusicOn) { if (currentMusicPlayer != null && currentMusicPlayer.isPlaying()) {
this.musicPlayer.stop(); currentMusicPlayer.stop();
currentMusicPlayer = null;
setIcon(this.iconOff); setIcon(this.iconOff);
} else { } else {
this.musicPlayer.play(); this.musicPlayer.play();
setIcon(this.iconOn); setIcon(this.iconOn);
currentMusicPlayer = this.musicPlayer;
} }
this.isMusicOn = !this.isMusicOn;
}); });
} }
} }

View File

@ -14,7 +14,6 @@ public class MusicPlayer {
/** /**
* Constructs a MusicPlayer with the specified file path. * Constructs a MusicPlayer with the specified file path.
*
* @param filePath The path to the music file to be played. * @param filePath The path to the music file to be played.
*/ */
public MusicPlayer(String filePath) { public MusicPlayer(String filePath) {
@ -50,7 +49,6 @@ public class MusicPlayer {
/** /**
* Checks if the music is currently playing. * Checks if the music is currently playing.
*
* @return true if the music is playing, false otherwise. * @return true if the music is playing, false otherwise.
*/ */
public boolean isPlaying() { public boolean isPlaying() {

View File

@ -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);
}
}
}
}

View File

@ -2,5 +2,70 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
public class PlayMenuView extends JPanel { 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();
}
} }

View File

@ -23,7 +23,7 @@ public class Window extends JFrame {
public Window() { public Window() {
super(PROGRAM_TITLE); super(PROGRAM_TITLE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(this.MIN_WINDOW_SIZE); this.setMinimumSize(MIN_WINDOW_SIZE);
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
getContentPane().setBackground(new Color(54, 91, 109)); getContentPane().setBackground(new Color(54, 91, 109));
} }