Modification du menu + Optimisation

This commit is contained in:
Moncef STITI 2024-04-09 17:13:23 +02:00
parent e0b6bc7de8
commit fa0273f597
10 changed files with 117 additions and 89 deletions

5
src/DialogManager.java Normal file
View File

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

View File

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

68
src/HomeView.java Normal file
View File

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

View File

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

View File

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

View File

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

6
src/PlayMenuView.java Normal file
View File

@ -0,0 +1,6 @@
import javax.swing.*;
import java.awt.*;
public class PlayMenuView extends JPanel {
// à faire
}

View File

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

11
src/Title.java Normal file
View File

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

View File

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