Ajout d'une musique de fond dans le menu

This commit is contained in:
Moncef STITI 2024-04-07 17:14:17 +02:00
parent ff4be58baa
commit 9879dbee8c
8 changed files with 216 additions and 30 deletions

BIN
audio/musiqueDeFond.wav Normal file

Binary file not shown.

BIN
img/iconeAudio.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
img/iconeAudioMuted.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

61
src/Button.java Normal file
View File

@ -0,0 +1,61 @@
import javax.swing.*;
import java.awt.*;
/**
* Class containing custom settings for JButtons used in the application.
* @version 1.0
* @author Moncef STITI
* @author Marco ORFAO
*/
public class Button extends JButton {
/**
* Constructor
* @param text The text of the button
*/
public Button (String text) {
super(text);
setFont(new Font("Arial", Font.BOLD, 15));
setBackground(new Color(96, 175, 255));
}
/**
* Constructor
* @param text The text of the button
* @param dimension The dimension of the button
*/
public Button(String text, Dimension dimension) {
super(text);
setPreferredSize(dimension);
setFont(new Font("Arial", Font.BOLD, 20));
setBackground(new Color(96, 175, 255));
}
/**
* Constructor
* @param text The text of the button
* @param dimension The dimension of the button
* @param font The font of the text in the button
*/
public Button(String text, Dimension dimension, Font font) {
super(text);
setPreferredSize(dimension);
setFont(font);
setBackground(new Color(96, 175, 255));
}
/**
* Constructor
* @param text The text of the button
* @param dimension The dimension of the button
* @param font The font of the text in the button
* @param color The background color of the button
*/
public Button(String text, Dimension dimension, Font font, Color color) {
super(text);
setPreferredSize(dimension);
setFont(font);
setBackground(color);
}
}

5
src/Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main{
public static void main(String[] args) {
MenuView menu = new MenuView();
}
}

View File

@ -1,30 +1,45 @@
import javax.swing.*;
import java.awt.*;
public class Menu extends JFrame {
// Attributs
/**
* A menu view for a Sudoku application.
* @version 1.0
* @author Moncef STITI
* @author Marco ORFAO
*/
public class MenuView extends JFrame {
private String scheminIconAudio = new String("../img/iconeAudio.png");
private String scheminIconAudioMuted = new String("../img/iconeAudioMuted.png");
private String scheminMusique = new String("../audio/musiqueDeFond.wav");
private ImageIcon iconAudio = new ImageIcon(scheminIconAudio);
private ImageIcon iconAudioMuted = new ImageIcon(scheminIconAudioMuted);
private ImageIcon imageSudoku = new ImageIcon("../img/sudoku.png"); // Image Sudoku
private Dimension tailleFenetre = new Dimension(1000,700); // Taille de la fenêtre
private Dimension tailleBouton = new Dimension(300, 60); // Taille des boutons
private Color couleurFond = new Color(54, 91, 109); // Couleur du fond de la fenêtre
private Color couleurTexteTitre = Color.WHITE; // Couleur du texte du titre et sous-titre
private int tailleTitre = 75; // Taille du texte du titre
private int tailleSousTitre = 24; // Taille du texte du sous-titre
private int tailleTexteBouton = 24; // Taille du texte des boutons
private Dimension tailleFenetre = new Dimension(1000,700); // Taille de la fenêtre
private Font policeTitre = new Font("Copperplate", Font.BOLD, tailleTitre); // Police du titre
private Font policeSousTitre = new Font("Copperplate", Font.PLAIN, tailleSousTitre); // Police du sous-titre
private Font policeBouton = new Font("Copperplate", Font.BOLD, tailleTexteBouton); // Police des boutons
private Font policeTitre = new Font("Copperplate", Font.BOLD, 75); // Police du titre
private Font policeSousTitre = new Font("Copperplate", Font.PLAIN, 24); // Police du sous-titre
private Font policeBouton = new Font("Copperplate", Font.BOLD, 24); // Police des boutons
private String[] boutonTextes = {"Jouer", "Générer une grille", "Résoudre une grille", "Quitter"}; // Textes des boutons
private JLabel[] labels = {new JLabel("Sudoku Solver", SwingConstants.CENTER), // Texte du titre et sous-titre
new JLabel("Par Moncef & Marco", SwingConstants.CENTER)};
private JLabel[] labels = {new JLabel("Sudoku Solver", SwingConstants.CENTER), // Texte du titre
new JLabel("Par Moncef & Marco", SwingConstants.CENTER)}; // Texte du sous-titre
// Constructeurs
public Menu() {
// Création et configuration de la fenêtre (taille, fermeture)
/**
* Constructs a MenuView.
* Initializes and configures the menu window with buttons and controls.
*/
public MenuView() {
// Création et configuration de la fenêtre
JFrame fenetre = new JFrame("Sudoku - Menu principal");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setMinimumSize(tailleFenetre);
fenetre.getContentPane().setBackground(couleurFond);
fenetre.setLayout(new BorderLayout());
BorderLayout gestionnaireBorderLayout = new BorderLayout();
fenetre.setLayout(gestionnaireBorderLayout);
// Création et ajout du panneau de titre
JPanel panneauTitre = new JPanel(new GridLayout(2, 1));
@ -40,32 +55,31 @@ public class Menu extends JFrame {
// Création et ajout du panneau de boutons
JPanel panneauBouton = new JPanel();
panneauBouton.setLayout(new GridLayout(boutonTextes.length, 1, 0, 10));
panneauBouton.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // Permet de ne pas coller les boutons aux bords de la fenêtre
GridLayout gestionnairePanneauBouton = new GridLayout(boutonTextes.length, 1, 0, 10);
panneauBouton.setLayout(gestionnairePanneauBouton);
panneauBouton.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
panneauBouton.setBackground(couleurFond);
JButton[] boutons = new JButton[boutonTextes.length];
Button[] boutons = new Button[boutonTextes.length];
for (int i = 0; i < boutons.length; i++) {
boutons[i] = new JButton(boutonTextes[i]);
boutons[i].setFont(policeBouton);
boutons[i].setPreferredSize(tailleBouton);
boutons[i] = new Button(boutonTextes[i], tailleBouton, policeBouton, couleurFond);
panneauBouton.add(boutons[i]);
}
fenetre.add(panneauBouton, BorderLayout.WEST);
// Ajout de l'image "sudoku.png"
ImageIcon imageSudoku = new ImageIcon("../img/sudoku.png");
JLabel imageLabel = new JLabel(imageSudoku);
fenetre.add(imageLabel, BorderLayout.EAST);
// Ajout du bouton de contrôle de la musique
MusicButton musicButton = new MusicButton(scheminIconAudio, scheminIconAudioMuted, scheminMusique);
FlowLayout gestionnaireControlPanel = new FlowLayout(FlowLayout.RIGHT);
JPanel controlPanel = new JPanel(gestionnaireControlPanel);
controlPanel.setBackground(couleurFond);
controlPanel.add(musicButton);
fenetre.add(controlPanel, BorderLayout.SOUTH);
fenetre.pack();
fenetre.setLocationRelativeTo(null);
fenetre.setVisible(true);
}
/* À SUPPRIMER APRÈS LA PHASE DE DEBUG
public static void main(String[] args) {
Menu menu = new Menu();
}
*/
}
}

47
src/MusicButton.java Normal file
View File

@ -0,0 +1,47 @@
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;
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.
*/
public MusicButton(String onIconPath, String offIconPath, String musicFilePath) {
iconOn = new ImageIcon(onIconPath);
iconOff = new ImageIcon(offIconPath);
setIcon(iconOff);
isMusicOn = false;
musicPlayer = new MusicPlayer(musicFilePath);
addActionListener(e -> {
if (isMusicOn) {
musicPlayer.stop();
setIcon(iconOff);
} else {
musicPlayer.play();
setIcon(iconOn);
}
isMusicOn = !isMusicOn;
});
}
}

59
src/MusicPlayer.java Normal file
View File

@ -0,0 +1,59 @@
import java.io.File;
import javax.sound.sampled.*;
/**
* Class containign a simple music player that allows playing and stopping music.
* @version 1.0
* @author Moncef STITI
* @author Marco ORFAO
*/
public class MusicPlayer {
private Clip clip;
private boolean isPlaying;
/**
* Constructs a MusicPlayer with the specified file path.
*
* @param filePath The path to the music file to be played.
*/
public MusicPlayer(String filePath) {
try {
File file = new File(filePath);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Starts playing the music.
*/
public void play() {
if (clip != null && !isPlaying) {
clip.start();
isPlaying = true;
}
}
/**
* Stops the music.
*/
public void stop() {
if (clip != null && isPlaying) {
clip.stop();
isPlaying = false;
}
}
/**
* Checks if the music is currently playing.
*
* @return true if the music is playing, false otherwise.
*/
public boolean isPlaying() {
return isPlaying;
}
}