test pour faire le plateau+nouveau menu dans testV1+ probleme compilation
This commit is contained in:
45
TestV1/src/fr/monkhanny/dorfromantik/Main.java
Normal file
45
TestV1/src/fr/monkhanny/dorfromantik/Main.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package fr.monkhanny.dorfromantik;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
import fr.monkhanny.dorfromantik.controller.MainMenuResizeController;
|
||||
import fr.monkhanny.dorfromantik.controller.MainMenuButtonController;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
import fr.monkhanny.dorfromantik.enums.Musics;
|
||||
import fr.monkhanny.dorfromantik.listeners.SettingsWindowListener;
|
||||
import fr.monkhanny.dorfromantik.gui.SettingsPanel;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
/**
|
||||
* Classe principale du jeu
|
||||
* @version 1.0
|
||||
* @author Moncef STITI
|
||||
* @see MainMenu
|
||||
* @see MainMenuResizeController
|
||||
*/
|
||||
public class Main {
|
||||
/**
|
||||
* Méthode principale du jeu
|
||||
* @param args Tableau de String contenant les arguments passé en paramètre au programme
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// Créer la fenêtre des paramètres
|
||||
JFrame settingsFrame = new JFrame("Paramètres");
|
||||
|
||||
// Menu principal
|
||||
MusicPlayer.loadMusic(Musics.MAIN_MENU_MUSIC);
|
||||
MusicPlayer.playMusic();
|
||||
MainMenu mainMenu = new MainMenu();
|
||||
MainMenuResizeController MainMenuResizeController = new MainMenuResizeController(mainMenu);
|
||||
MainMenuButtonController MainMenuButtonController = new MainMenuButtonController(mainMenu,settingsFrame);
|
||||
|
||||
|
||||
// Fenêtre des paramètres
|
||||
SettingsWindowListener windowListener = new SettingsWindowListener(mainMenu, settingsFrame);
|
||||
SettingsPanel settingsPanel = new SettingsPanel(mainMenu, settingsFrame);
|
||||
settingsFrame.addWindowListener(windowListener);
|
||||
settingsFrame.add(settingsPanel);
|
||||
settingsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
}
|
||||
}
|
54
TestV1/src/fr/monkhanny/dorfromantik/Options.java
Normal file
54
TestV1/src/fr/monkhanny/dorfromantik/Options.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package fr.monkhanny.dorfromantik;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
|
||||
public class Options {
|
||||
|
||||
/**
|
||||
* Taille de police de base pour les titres du menu principal
|
||||
*/
|
||||
public static final float BASE_TITLE_FONT_SIZE = 80f;
|
||||
|
||||
/**
|
||||
* Taille de police de base pour les boutons du menu principal
|
||||
*/
|
||||
public static final float BASE_BUTTON_FONT_SIZE = 45f;
|
||||
|
||||
/**
|
||||
* Niveau de volume par défaut
|
||||
*/
|
||||
public static final int DEFAULT_VOLUME = 60;
|
||||
|
||||
/**
|
||||
* Musique en sourdine ou non
|
||||
*/
|
||||
public static boolean MUSIC_MUTED = false;
|
||||
|
||||
/**
|
||||
* Sons en sourdine ou non
|
||||
*/
|
||||
public static boolean SOUNDS_MUTED = false;
|
||||
|
||||
/**
|
||||
* Couleur de subrillance des boutons
|
||||
*/
|
||||
public static final Color BUTTON_HOVER_COLOR = new Color(70, 130, 180);
|
||||
|
||||
|
||||
public static final float HOVER_FONT_SCALE = 1.1f;
|
||||
public static final int ANIMATION_STEPS = 10;
|
||||
public static final int ANIMATION_DELAY = 15;
|
||||
|
||||
/**
|
||||
* Volume de la musique
|
||||
*/
|
||||
public static int MUSIC_VOLUME = 60;
|
||||
|
||||
/**
|
||||
* Volume des bruitages
|
||||
*/
|
||||
public static int SOUNDS_VOLUME = 60;
|
||||
|
||||
public static final Dimension MINIMUM_FRAME_SIZE = new Dimension(700, 700);
|
||||
}
|
48
TestV1/src/fr/monkhanny/dorfromantik/components/Button.java
Normal file
48
TestV1/src/fr/monkhanny/dorfromantik/components/Button.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package fr.monkhanny.dorfromantik.components;
|
||||
|
||||
import fr.monkhanny.dorfromantik.utils.FontManager;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Button {
|
||||
|
||||
public static JButton createCustomTextButton(String text, float fontSize) {
|
||||
JButton button = new JButton(text);
|
||||
button.setFocusPainted(false); // Retirer le focus
|
||||
button.setBackground(new Color(102, 178, 255)); // Couleur de fond
|
||||
button.setForeground(Color.WHITE); // Couleur du texte
|
||||
button.setFont(FontManager.getButtonFont(fontSize)); // Appliquer la police du bouton
|
||||
button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); // Espacement autour du texte du bouton
|
||||
return button;
|
||||
}
|
||||
|
||||
public static JButton createCustomIconButton(String iconPath) {
|
||||
// Créer le bouton
|
||||
JButton button = new JButton();
|
||||
button.setFocusPainted(false); // Retirer le focus
|
||||
|
||||
// Charger l'icône depuis le chemin spécifié
|
||||
ImageIcon icon = new ImageIcon(iconPath);
|
||||
|
||||
// Calculer automatiquement la taille de l'icône pour l'adapter à la taille du bouton
|
||||
int buttonWidth = 100; // Taille du bouton (largeur)
|
||||
int buttonHeight = 100; // Taille du bouton (hauteur)
|
||||
|
||||
// Vous pouvez ajuster ces valeurs ou les calculer dynamiquement en fonction de la taille du bouton
|
||||
Image img = icon.getImage();
|
||||
Image resizedImage = img.getScaledInstance(buttonWidth, buttonHeight, Image.SCALE_SMOOTH);
|
||||
button.setIcon(new ImageIcon(resizedImage));
|
||||
|
||||
// Optionnel : changer la couleur de fond ou de bordure si nécessaire
|
||||
button.setBackground(new Color(102, 178, 255)); // Couleur de fond (facultatif)
|
||||
|
||||
// Retirer la bordure du bouton (facultatif)
|
||||
button.setBorder(BorderFactory.createEmptyBorder());
|
||||
|
||||
// Redimensionner le bouton pour s'adapter à l'icône si nécessaire
|
||||
button.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
|
||||
|
||||
return button;
|
||||
}
|
||||
}
|
27
TestV1/src/fr/monkhanny/dorfromantik/components/Title.java
Normal file
27
TestV1/src/fr/monkhanny/dorfromantik/components/Title.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package fr.monkhanny.dorfromantik.components;
|
||||
|
||||
import fr.monkhanny.dorfromantik.utils.FontManager;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Title extends JLabel {
|
||||
|
||||
public Title(String text, float fontSize) {
|
||||
super(text, SwingConstants.CENTER);
|
||||
setFont(FontManager.getTitleFont(fontSize));
|
||||
setForeground(Color.WHITE);
|
||||
setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
|
||||
}
|
||||
|
||||
public Title(String text, float fontSize, Color textColor) {
|
||||
super(text, SwingConstants.CENTER);
|
||||
setFont(FontManager.getTitleFont(fontSize));
|
||||
setForeground(textColor);
|
||||
setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
|
||||
}
|
||||
|
||||
public void updateTitleFont(float fontSize) {
|
||||
setFont(FontManager.getTitleFont(fontSize));
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ButtonHoverAnimationListener implements ActionListener {
|
||||
|
||||
private int step = 0;
|
||||
private final float scaleIncrement;
|
||||
private final boolean entering;
|
||||
private final JButton button;
|
||||
private final Color originalColor;
|
||||
private final Font originalFont;
|
||||
private float currentScale = 1.0f;
|
||||
|
||||
public ButtonHoverAnimationListener(boolean entering, JButton button, Color originalColor, Font originalFont) {
|
||||
this.entering = entering;
|
||||
this.button = button;
|
||||
this.originalColor = originalColor;
|
||||
this.originalFont = originalFont;
|
||||
this.scaleIncrement = (Options.HOVER_FONT_SCALE - 1.0f) / Options.ANIMATION_STEPS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
currentScale = entering ? (1.0f + step * scaleIncrement) : (Options.HOVER_FONT_SCALE - step * scaleIncrement);
|
||||
|
||||
button.setForeground(entering ? Options.BUTTON_HOVER_COLOR : originalColor);
|
||||
button.setFont(originalFont.deriveFont(originalFont.getSize2D() * currentScale));
|
||||
|
||||
step++;
|
||||
if (step >= Options.ANIMATION_STEPS) {
|
||||
((Timer) e.getSource()).stop();
|
||||
if (!entering) {
|
||||
button.setFont(originalFont); // Restore the original font
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonHoverAnimator;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
import fr.monkhanny.dorfromantik.enums.Sounds;
|
||||
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class ButtonHoverListener extends MouseAdapter {
|
||||
|
||||
private final ButtonHoverAnimator animator;
|
||||
|
||||
public ButtonHoverListener(ButtonHoverAnimator animator) {
|
||||
this.animator = animator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
animator.startAnimation(true);
|
||||
MusicPlayer.loadSound(Sounds.SOUNDS1); // Charge le son
|
||||
MusicPlayer.playSound(); // Joue le son
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
animator.startAnimation(false);
|
||||
}
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.gui.SettingsPanel;
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonPanel;
|
||||
import fr.monkhanny.dorfromantik.listeners.SettingsWindowListener;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
|
||||
public class MainMenuButtonController implements ActionListener {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
|
||||
private JFrame settingsFrame;
|
||||
|
||||
public MainMenuButtonController(MainMenu mainMenu, JFrame settingsFrame) {
|
||||
this.mainMenu = mainMenu;
|
||||
// Ajouter les écouteurs d'événements sur les boutons
|
||||
ButtonPanel buttonPanel = mainMenu.getButtonPanel();
|
||||
|
||||
// Attacher les actions aux boutons
|
||||
buttonPanel.getNewGameButton().addActionListener(this);
|
||||
buttonPanel.getContinueGameButton().addActionListener(this);
|
||||
buttonPanel.getHowToPlayButton().addActionListener(this);
|
||||
buttonPanel.getSettingsButton().addActionListener(this);
|
||||
buttonPanel.getExitButton().addActionListener(this);
|
||||
|
||||
// Créer la fenêtre des paramètres
|
||||
this.settingsFrame = settingsFrame;
|
||||
this.settingsFrame.setLocationRelativeTo(null);
|
||||
this.settingsFrame.setVisible(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String command = e.getActionCommand();
|
||||
|
||||
switch (command) {
|
||||
case "Nouvelle partie":
|
||||
startNewGame();
|
||||
break;
|
||||
case "Continuer une partie":
|
||||
continueGame();
|
||||
break;
|
||||
case "Comment jouer ?":
|
||||
showHowToPlay();
|
||||
break;
|
||||
case "Paramètres":
|
||||
openSettings();
|
||||
break;
|
||||
case "Quitter":
|
||||
exitGame();
|
||||
break;
|
||||
default:
|
||||
System.out.println("Commande inconnue: " + command);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void startNewGame() {
|
||||
System.out.println("Démarrer une nouvelle partie...");
|
||||
// Logic to start a new game
|
||||
}
|
||||
|
||||
private void continueGame() {
|
||||
System.out.println("Continuer une partie...");
|
||||
// Logic to continue the game
|
||||
}
|
||||
|
||||
private void showHowToPlay() {
|
||||
System.out.println("Afficher comment jouer...");
|
||||
// Logic to show how to play
|
||||
}
|
||||
|
||||
private void exitGame() {
|
||||
System.exit(0); // Fermer l'application
|
||||
}
|
||||
|
||||
private void openSettings() {
|
||||
// Récupérer la taille et la position de la fenêtre du menu principal
|
||||
Dimension mainMenuSize = this.mainMenu.getSize();
|
||||
Point mainMenuLocation = this.mainMenu.getLocation();
|
||||
|
||||
// Ajuster la fenêtre des paramètres pour qu'elle ait la même taille et position
|
||||
this.settingsFrame.setSize(mainMenuSize);
|
||||
this.settingsFrame.setLocation(mainMenuLocation);
|
||||
|
||||
// Cacher la fenêtre du menu principal
|
||||
this.mainMenu.setVisible(false);
|
||||
|
||||
// Afficher la fenêtre des paramètres
|
||||
this.settingsFrame.setVisible(true);
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonPanel;
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonHoverAnimator;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class MainMenuMouseController {
|
||||
|
||||
private final ButtonPanel buttonPanel;
|
||||
|
||||
public MainMenuMouseController(ButtonPanel buttonPanel) {
|
||||
this.buttonPanel = buttonPanel;
|
||||
initMouseListeners();
|
||||
}
|
||||
|
||||
private void initMouseListeners() {
|
||||
addButtonHoverListener(buttonPanel.getNewGameButton());
|
||||
addButtonHoverListener(buttonPanel.getContinueGameButton());
|
||||
addButtonHoverListener(buttonPanel.getHowToPlayButton());
|
||||
addButtonHoverListener(buttonPanel.getSettingsButton());
|
||||
addButtonHoverListener(buttonPanel.getExitButton());
|
||||
}
|
||||
|
||||
private void addButtonHoverListener(JButton button) {
|
||||
ButtonHoverAnimator animator = new ButtonHoverAnimator(button);
|
||||
button.addMouseListener(new ButtonHoverListener(animator));
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
|
||||
public class MainMenuResizeController {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
private MainMenuResizeHandler resizeHandler;
|
||||
|
||||
public MainMenuResizeController(MainMenu mainMenu) {
|
||||
this.mainMenu = mainMenu;
|
||||
this.resizeHandler = new MainMenuResizeHandler(mainMenu);
|
||||
addComponentListener();
|
||||
}
|
||||
|
||||
private void addComponentListener() {
|
||||
mainMenu.addComponentListener(resizeHandler);
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonHoverAnimator;
|
||||
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import javax.swing.JButton;
|
||||
|
||||
public class MainMenuResizeHandler extends ComponentAdapter {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
|
||||
public MainMenuResizeHandler(MainMenu mainMenu) {
|
||||
this.mainMenu = mainMenu;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
int mainMenuWidth = mainMenu.getWidth();
|
||||
|
||||
// Ajuster la taille de la police du titre en fonction de la taille de la fenêtre
|
||||
float newFontSize = Options.BASE_TITLE_FONT_SIZE * (mainMenuWidth / 900f);
|
||||
mainMenu.getTitleLabel().updateTitleFont(newFontSize);
|
||||
|
||||
// Mettre à jour les polices des boutons
|
||||
mainMenu.getButtonPanel().updateButtonFonts(mainMenuWidth);
|
||||
ButtonHoverAnimator.updateOriginalFont(mainMenuWidth / 30f); // On passe la nouvelle taille de police pour chaque bouton
|
||||
}
|
||||
}
|
18
TestV1/src/fr/monkhanny/dorfromantik/enums/Fonts.java
Normal file
18
TestV1/src/fr/monkhanny/dorfromantik/enums/Fonts.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package fr.monkhanny.dorfromantik.enums;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public enum Fonts {
|
||||
TITLE, BUTTON;
|
||||
|
||||
public String getFontPath() {
|
||||
switch (this) {
|
||||
case TITLE:
|
||||
return "./ressources/fonts/Contage-Black.ttf";
|
||||
case BUTTON:
|
||||
return "./ressources/fonts/Contage-Regular.ttf";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected value: " + this);
|
||||
}
|
||||
}
|
||||
}
|
17
TestV1/src/fr/monkhanny/dorfromantik/enums/Images.java
Normal file
17
TestV1/src/fr/monkhanny/dorfromantik/enums/Images.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package fr.monkhanny.dorfromantik.enums;
|
||||
|
||||
|
||||
public enum Images {
|
||||
SETTINGS_ICON, EXIT_ICON;
|
||||
|
||||
public String getImagePath() {
|
||||
switch (this) {
|
||||
case SETTINGS_ICON:
|
||||
return "./ressources/images/Icone/SettingsIcon.png";
|
||||
case EXIT_ICON:
|
||||
return "./ressources/images/Icone/ExitIcon.png";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected value: " + this);
|
||||
}
|
||||
}
|
||||
}
|
14
TestV1/src/fr/monkhanny/dorfromantik/enums/Musics.java
Normal file
14
TestV1/src/fr/monkhanny/dorfromantik/enums/Musics.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package fr.monkhanny.dorfromantik.enums;
|
||||
|
||||
public enum Musics {
|
||||
MAIN_MENU_MUSIC;
|
||||
|
||||
public String getSoundsPath() {
|
||||
switch (this) {
|
||||
case MAIN_MENU_MUSIC:
|
||||
return "./ressources/sounds/Music/mainMenuMusic.wav";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected value: " + this);
|
||||
}
|
||||
}
|
||||
}
|
16
TestV1/src/fr/monkhanny/dorfromantik/enums/Sounds.java
Normal file
16
TestV1/src/fr/monkhanny/dorfromantik/enums/Sounds.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package fr.monkhanny.dorfromantik.enums;
|
||||
|
||||
public enum Sounds {
|
||||
SOUNDS1, SOUNDS2;
|
||||
|
||||
public String getSoundsPath() {
|
||||
switch (this) {
|
||||
case SOUNDS1:
|
||||
return "./ressources/sounds/SFX/1.wav";
|
||||
case SOUNDS2:
|
||||
return "./ressources/sounds/SFX/2.wav";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected value: " + this);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ButtonHoverAnimator {
|
||||
|
||||
private final JButton button;
|
||||
private final Color originalColor;
|
||||
private static Font originalFont;
|
||||
private Timer animationTimer;
|
||||
private float currentScale = 1.0f;
|
||||
|
||||
public ButtonHoverAnimator(JButton button) {
|
||||
this.button = button;
|
||||
this.originalColor = button.getForeground();
|
||||
this.originalFont = button.getFont();
|
||||
}
|
||||
|
||||
public void startAnimation(boolean entering) {
|
||||
if (animationTimer != null && animationTimer.isRunning()) {
|
||||
animationTimer.stop();
|
||||
}
|
||||
|
||||
// Create a new ActionListener instance
|
||||
animationTimer = new Timer(Options.ANIMATION_DELAY, new ButtonHoverAnimationListener(entering, button, originalColor, originalFont));
|
||||
animationTimer.start();
|
||||
}
|
||||
|
||||
public static void updateOriginalFont(float newFontSize) {
|
||||
originalFont = originalFont.deriveFont(newFontSize);
|
||||
}
|
||||
|
||||
}
|
87
TestV1/src/fr/monkhanny/dorfromantik/gui/ButtonPanel.java
Normal file
87
TestV1/src/fr/monkhanny/dorfromantik/gui/ButtonPanel.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import fr.monkhanny.dorfromantik.utils.FontManager;
|
||||
import fr.monkhanny.dorfromantik.components.Button;
|
||||
import fr.monkhanny.dorfromantik.controller.MainMenuMouseController;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class ButtonPanel extends JPanel {
|
||||
|
||||
private JButton newGameButton;
|
||||
private JButton continueGameButton;
|
||||
private JButton howToPlayButton;
|
||||
private JButton settingsButton;
|
||||
private JButton exitButton;
|
||||
|
||||
public ButtonPanel(float fontSize) {
|
||||
// Paramétrage de l'apparence du panneau
|
||||
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
this.setOpaque(false); // Rendre le panneau transparent
|
||||
this.setBorder(BorderFactory.createEmptyBorder(50, 30, 30, 30)); // Marge à gauche et en bas
|
||||
|
||||
// Espacement vertical extensible pour centrer les boutons principaux verticalement
|
||||
this.add(Box.createVerticalGlue());
|
||||
|
||||
// Créer les boutons avec un style personnalisé
|
||||
newGameButton = Button.createCustomTextButton("Nouvelle partie", fontSize);
|
||||
continueGameButton = Button.createCustomTextButton("Continuer une partie", fontSize);
|
||||
howToPlayButton = Button.createCustomTextButton("Comment jouer ?", fontSize);
|
||||
settingsButton = Button.createCustomTextButton("Paramètres", fontSize);
|
||||
exitButton = Button.createCustomTextButton("Quitter", fontSize);
|
||||
|
||||
// Ajouter les boutons au panneau
|
||||
this.add(newGameButton);
|
||||
this.add(Box.createVerticalStrut(10)); // Espace entre les boutons
|
||||
this.add(continueGameButton);
|
||||
this.add(Box.createVerticalStrut(10));
|
||||
this.add(howToPlayButton);
|
||||
this.add(Box.createVerticalStrut(10));
|
||||
this.add(settingsButton);
|
||||
this.add(Box.createVerticalStrut(10));
|
||||
this.add(exitButton);
|
||||
|
||||
// Espacement extensible pour maintenir les icônes en bas
|
||||
this.add(Box.createVerticalGlue());
|
||||
|
||||
MainMenuMouseController gestionSouris = new MainMenuMouseController(this);
|
||||
}
|
||||
|
||||
public JButton getNewGameButton() {
|
||||
return newGameButton;
|
||||
}
|
||||
|
||||
public JButton getContinueGameButton() {
|
||||
return continueGameButton;
|
||||
}
|
||||
|
||||
public JButton getHowToPlayButton() {
|
||||
return howToPlayButton;
|
||||
}
|
||||
|
||||
public JButton getSettingsButton() {
|
||||
return settingsButton;
|
||||
}
|
||||
|
||||
public JButton getExitButton() {
|
||||
return exitButton;
|
||||
}
|
||||
|
||||
public List<JButton> getButtons() {
|
||||
return Arrays.asList(newGameButton, continueGameButton, howToPlayButton, settingsButton, exitButton);
|
||||
}
|
||||
|
||||
public void updateButtonFonts(int windowWidth) {
|
||||
// Mettre à jour la police des boutons avec la taille ajustée
|
||||
float newFontSize = windowWidth / 30f;
|
||||
newGameButton.setFont(FontManager.getTitleFont(newFontSize));
|
||||
continueGameButton.setFont(FontManager.getTitleFont(newFontSize));
|
||||
howToPlayButton.setFont(FontManager.getTitleFont(newFontSize));
|
||||
settingsButton.setFont(FontManager.getTitleFont(newFontSize));
|
||||
exitButton.setFont(FontManager.getTitleFont(newFontSize));
|
||||
}
|
||||
}
|
56
TestV1/src/fr/monkhanny/dorfromantik/gui/MainMenu.java
Normal file
56
TestV1/src/fr/monkhanny/dorfromantik/gui/MainMenu.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import fr.monkhanny.dorfromantik.utils.FontManager;
|
||||
import fr.monkhanny.dorfromantik.utils.ImageLoader;
|
||||
import fr.monkhanny.dorfromantik.enums.Fonts;
|
||||
import fr.monkhanny.dorfromantik.components.Title;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
public class MainMenu extends JFrame {
|
||||
|
||||
private Title titleLabel;
|
||||
private ButtonPanel buttonPanel;
|
||||
|
||||
public MainMenu() {
|
||||
// Charger les polices pour le titre et les boutons
|
||||
FontManager.loadCustomFont(Fonts.TITLE); // Charge la police pour le titre
|
||||
FontManager.loadCustomFont(Fonts.BUTTON); // Charge la police pour les boutons
|
||||
|
||||
// Paramétrage de la fenêtre principale
|
||||
this.setTitle("Dorfromantik - Menu Principal");
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
super.setIconImage(ImageLoader.APPLICATION_ICON);
|
||||
this.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||
this.setSize(1200, 800);
|
||||
this.setLocationRelativeTo(null); // Centrer la fenêtre
|
||||
this.setLayout(new BorderLayout());
|
||||
|
||||
// Arrière plan du menu principal
|
||||
JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/background.jpg"));
|
||||
background.setLayout(new BorderLayout());
|
||||
this.setContentPane(background);
|
||||
|
||||
// Ajouter le titre en haut au centre
|
||||
this.titleLabel = new Title("Dorfromantik", Options.BASE_TITLE_FONT_SIZE);
|
||||
background.add(titleLabel, BorderLayout.NORTH);
|
||||
|
||||
// Panneau des boutons avec style personnalisé
|
||||
this.buttonPanel = new ButtonPanel(Options.BASE_BUTTON_FONT_SIZE);
|
||||
background.add(buttonPanel, BorderLayout.WEST);
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public Title getTitleLabel() {
|
||||
return titleLabel;
|
||||
}
|
||||
|
||||
public ButtonPanel getButtonPanel() {
|
||||
return buttonPanel;
|
||||
}
|
||||
}
|
142
TestV1/src/fr/monkhanny/dorfromantik/gui/SettingsPanel.java
Normal file
142
TestV1/src/fr/monkhanny/dorfromantik/gui/SettingsPanel.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.components.Title;
|
||||
import fr.monkhanny.dorfromantik.listeners.*;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
public class SettingsPanel extends JPanel {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
private JFrame settingsFrame;
|
||||
|
||||
public SettingsPanel(MainMenu mainMenu, JFrame settingsFrame) {
|
||||
this.mainMenu = mainMenu;
|
||||
this.settingsFrame = settingsFrame;
|
||||
|
||||
initializeSettingsFrame();
|
||||
setupBackground();
|
||||
setupMainPanel();
|
||||
}
|
||||
|
||||
private void initializeSettingsFrame() {
|
||||
settingsFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||
}
|
||||
|
||||
private void setupBackground() {
|
||||
JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
|
||||
background.setLayout(new GridBagLayout());
|
||||
this.setLayout(new BorderLayout());
|
||||
this.add(background);
|
||||
}
|
||||
|
||||
private void setupMainPanel() {
|
||||
JPanel mainPanel = createMainPanel();
|
||||
JLabel title = createTitle();
|
||||
|
||||
mainPanel.add(title, createGridBagConstraints(0, 0, 2));
|
||||
mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 1, 1));
|
||||
|
||||
// Musique Panel
|
||||
JSlider musicSlider = new JSlider(0, 100, Options.MUSIC_MUTED ? 0 : Options.MUSIC_VOLUME);
|
||||
JPanel musicPanel = createSoundPanel("Musique", musicSlider, new MusicVolumeChangeListener(musicSlider), new MuteCheckBoxListener("Musique"));
|
||||
mainPanel.add(musicPanel, createGridBagConstraints(0, 2, 1));
|
||||
|
||||
mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 3, 1));
|
||||
|
||||
// SFX Panel
|
||||
JSlider sfxSlider = new JSlider(0, 100, Options.SOUNDS_MUTED ? 0 : Options.SOUNDS_VOLUME);
|
||||
JPanel sfxPanel = createSoundPanel("SFX", sfxSlider, new SoundsVolumeChangeListener(sfxSlider), new MuteCheckBoxListener("SFX"));
|
||||
mainPanel.add(sfxPanel, createGridBagConstraints(0, 4, 1));
|
||||
|
||||
// Close Button
|
||||
JButton closeButton = createCloseButton();
|
||||
mainPanel.add(closeButton, createGridBagConstraints(0, 5, 1));
|
||||
|
||||
// Add to background
|
||||
((JLabel) this.getComponent(0)).add(mainPanel);
|
||||
}
|
||||
|
||||
private JPanel createMainPanel() {
|
||||
JPanel mainPanel = new JPanel(new GridBagLayout());
|
||||
mainPanel.setOpaque(false);
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
private Title createTitle() {
|
||||
Title title = new Title("Paramètres", 70, Color.WHITE);
|
||||
return title;
|
||||
}
|
||||
|
||||
private GridBagConstraints createGridBagConstraints(int x, int y, int gridWidth) {
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.gridx = x;
|
||||
gbc.gridy = y;
|
||||
gbc.gridwidth = gridWidth;
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc.insets = new Insets(20, 30, 20, 30);
|
||||
return gbc;
|
||||
}
|
||||
|
||||
private JPanel createSoundPanel(String labelText, JSlider volumeSlider, ChangeListener sliderChangeListener, MuteCheckBoxListener muteCheckBoxListener) {
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
panel.setOpaque(false);
|
||||
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.insets = new Insets(10, 10, 10, 10);
|
||||
gbc.gridx = 0;
|
||||
|
||||
JCheckBox muteCheckBox = new JCheckBox();
|
||||
muteCheckBox.setSelected(!("Musique".equals(labelText) ? Options.MUSIC_MUTED : Options.SOUNDS_MUTED));
|
||||
muteCheckBox.addActionListener(muteCheckBoxListener);
|
||||
|
||||
panel.add(new JLabel(labelText), gbc);
|
||||
gbc.gridx++;
|
||||
panel.add(muteCheckBox, gbc);
|
||||
|
||||
volumeSlider.setPreferredSize(new Dimension(200, 50));
|
||||
volumeSlider.setMajorTickSpacing(50);
|
||||
volumeSlider.setPaintTicks(true);
|
||||
volumeSlider.setPaintLabels(true);
|
||||
volumeSlider.setFont(new Font("Roboto", Font.PLAIN, 16));
|
||||
volumeSlider.addChangeListener(sliderChangeListener);
|
||||
|
||||
gbc.gridx++;
|
||||
panel.add(createSliderPanel(volumeSlider), gbc);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private JPanel createSliderPanel(JSlider volumeSlider) {
|
||||
JPanel sliderPanel = new JPanel(new BorderLayout());
|
||||
sliderPanel.setOpaque(false);
|
||||
|
||||
JLabel lowLabel = new JLabel("Low");
|
||||
lowLabel.setFont(new Font("Roboto", Font.PLAIN, 18));
|
||||
sliderPanel.add(lowLabel, BorderLayout.WEST);
|
||||
|
||||
sliderPanel.add(volumeSlider, BorderLayout.CENTER);
|
||||
|
||||
JLabel highLabel = new JLabel("High");
|
||||
highLabel.setFont(new Font("Roboto", Font.PLAIN, 18));
|
||||
sliderPanel.add(highLabel, BorderLayout.EAST);
|
||||
|
||||
return sliderPanel;
|
||||
}
|
||||
|
||||
private JButton createCloseButton() {
|
||||
JButton closeButton = new JButton("Retour");
|
||||
closeButton.setFont(new Font("Roboto", Font.BOLD, 24));
|
||||
closeButton.setForeground(Color.BLACK);
|
||||
closeButton.setBackground(Color.WHITE);
|
||||
closeButton.setOpaque(true);
|
||||
closeButton.setPreferredSize(new Dimension(75, 50));
|
||||
closeButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
closeButton.addActionListener(new CloseButtonListener(mainMenu, settingsFrame));
|
||||
return closeButton;
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package fr.monkhanny.dorfromantik.listeners;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class CloseButtonListener implements ActionListener {
|
||||
private MainMenu mainMenu;
|
||||
private JFrame settingsFrame;
|
||||
|
||||
public CloseButtonListener(MainMenu mainMenu, JFrame settingsFrame) {
|
||||
this.mainMenu = mainMenu;
|
||||
this.settingsFrame = settingsFrame;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// Réafficher la fenêtre du menu principal
|
||||
mainMenu.setVisible(true);
|
||||
settingsFrame.setVisible(false); // Fermer la fenêtre des paramètres
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package fr.monkhanny.dorfromantik.listeners;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import javax.swing.JSlider;
|
||||
|
||||
public class MusicVolumeChangeListener implements ChangeListener {
|
||||
private JSlider slider;
|
||||
|
||||
public MusicVolumeChangeListener(JSlider slider) {
|
||||
this.slider = slider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
// Récupérer la valeur du slider spécifique
|
||||
Options.MUSIC_VOLUME = slider.getValue();
|
||||
MusicPlayer.setVolume(MusicPlayer.getMusicClip(), Options.MUSIC_VOLUME);
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package fr.monkhanny.dorfromantik.listeners;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class MuteCheckBoxListener implements ActionListener {
|
||||
private String label;
|
||||
|
||||
public MuteCheckBoxListener(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JCheckBox checkBox = (JCheckBox) e.getSource();
|
||||
|
||||
if ("Musique".equals(label)) {
|
||||
Options.MUSIC_MUTED = !checkBox.isSelected();
|
||||
if (Options.MUSIC_MUTED) {
|
||||
MusicPlayer.pauseMusic();
|
||||
} else {
|
||||
MusicPlayer.playMusic();
|
||||
}
|
||||
} else if ("SFX".equals(label)) {
|
||||
Options.SOUNDS_MUTED = !checkBox.isSelected();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package fr.monkhanny.dorfromantik.listeners;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import javax.swing.*;
|
||||
|
||||
public class SettingsWindowListener extends WindowAdapter {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
private JFrame settingsFrame;
|
||||
|
||||
public SettingsWindowListener(MainMenu mainMenu, JFrame settingsFrame) {
|
||||
this.mainMenu = mainMenu;
|
||||
this.settingsFrame = settingsFrame;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
// Réafficher la fenêtre du menu principal
|
||||
mainMenu.setVisible(true);
|
||||
settingsFrame.setVisible(false); // Fermer la fenêtre des paramètres
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package fr.monkhanny.dorfromantik.listeners;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import javax.swing.JSlider;
|
||||
|
||||
public class SoundsVolumeChangeListener implements ChangeListener {
|
||||
private JSlider slider;
|
||||
|
||||
public SoundsVolumeChangeListener(JSlider slider) {
|
||||
this.slider = slider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
// Récupérer la valeur du slider spécifique
|
||||
Options.SOUNDS_VOLUME = slider.getValue();
|
||||
MusicPlayer.setVolume(MusicPlayer.getSoundClip(), Options.SOUNDS_VOLUME);
|
||||
}
|
||||
}
|
33
TestV1/src/fr/monkhanny/dorfromantik/utils/FontLoader.java
Normal file
33
TestV1/src/fr/monkhanny/dorfromantik/utils/FontLoader.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Fonts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Classe utilitaire pour charger des polices à partir de fichiers.
|
||||
* @version 1.0
|
||||
* @author Moncef STITI
|
||||
* @see Fonts
|
||||
* @see Font
|
||||
*/
|
||||
public class FontLoader {
|
||||
|
||||
/**
|
||||
* Charge une police à partir du fichier spécifié.
|
||||
* @param fontEnumName Enumération de la police à charger.
|
||||
* @return La police chargée.
|
||||
* @throws IOException Si une erreur se produit lors de la lecture du fichier.
|
||||
* @throws FontFormatException Si une erreur se produit lors de la création de la police.
|
||||
*/
|
||||
public static Font loadFont(Fonts fontEnumName) throws IOException, FontFormatException {
|
||||
String fontFilePath = fontEnumName.getFontPath();
|
||||
File fontFile = new File(fontFilePath);
|
||||
Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
ge.registerFont(customFont);
|
||||
return customFont;
|
||||
}
|
||||
}
|
70
TestV1/src/fr/monkhanny/dorfromantik/utils/FontManager.java
Normal file
70
TestV1/src/fr/monkhanny/dorfromantik/utils/FontManager.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Fonts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FontManager {
|
||||
|
||||
private static Font titleFont;
|
||||
private static Font buttonFont;
|
||||
|
||||
// Charge et applique la police spécifique en fonction de Fonts
|
||||
public static void loadCustomFont(Fonts fontEnum) {
|
||||
try {
|
||||
Font loadedFont = FontLoader.loadFont(fontEnum);
|
||||
if (fontEnum == Fonts.TITLE) {
|
||||
titleFont = loadedFont;
|
||||
} else if (fontEnum == Fonts.BUTTON) {
|
||||
buttonFont = loadedFont;
|
||||
}
|
||||
} catch (IOException | FontFormatException e) {
|
||||
throw new RuntimeException("Failed to load font: " + fontEnum, e);
|
||||
}
|
||||
}
|
||||
|
||||
// Obtient la police du titre avec une taille spécifique
|
||||
public static Font getTitleFont(float size) {
|
||||
if (titleFont == null) {
|
||||
throw new IllegalStateException("Title font not loaded. Please load the font first.");
|
||||
}
|
||||
return titleFont.deriveFont(size);
|
||||
}
|
||||
|
||||
// Obtient la police du bouton avec une taille spécifique
|
||||
public static Font getButtonFont(float size) {
|
||||
if (buttonFont == null) {
|
||||
throw new IllegalStateException("Button font not loaded. Please load the font first.");
|
||||
}
|
||||
return buttonFont.deriveFont(size);
|
||||
}
|
||||
|
||||
// Ajuste la taille de la police du titre selon la taille du composant sans la modifier directement
|
||||
public static Font getAdjustedTitleFont(Component component, float minSize, float maxSize) {
|
||||
if (titleFont == null) {
|
||||
throw new IllegalStateException("Title font not loaded. Please load the font first.");
|
||||
}
|
||||
float newSize = Math.max(minSize, Math.min(maxSize, component.getWidth() / 12f));
|
||||
return titleFont.deriveFont(newSize);
|
||||
}
|
||||
|
||||
// Ajuste la taille de la police du bouton selon la taille du composant sans la modifier directement
|
||||
public static Font getAdjustedButtonFont(Component component, float minSize, float maxSize) {
|
||||
if (buttonFont == null) {
|
||||
throw new IllegalStateException("Button font not loaded. Please load the font first.");
|
||||
}
|
||||
float newSize = Math.max(minSize, Math.min(maxSize, component.getHeight() / 20f));
|
||||
return buttonFont.deriveFont(newSize);
|
||||
}
|
||||
|
||||
// Définir manuellement une police de titre personnalisée
|
||||
public static void setTitleFont(Font font) {
|
||||
titleFont = font;
|
||||
}
|
||||
|
||||
// Définir manuellement une police de bouton personnalisée
|
||||
public static void setButtonFont(Font font) {
|
||||
buttonFont = font;
|
||||
}
|
||||
}
|
35
TestV1/src/fr/monkhanny/dorfromantik/utils/ImageLoader.java
Normal file
35
TestV1/src/fr/monkhanny/dorfromantik/utils/ImageLoader.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
/**
|
||||
* Classe utilitaire pour charger des images à partir de fichiers.
|
||||
*
|
||||
* @version 1.0
|
||||
* @author Moncef STITI
|
||||
*/
|
||||
public class ImageLoader {
|
||||
/**
|
||||
* Icône de l'application.
|
||||
*/
|
||||
public static final Image APPLICATION_ICON = ImageLoader.loadImage("./ressources/images/Application/Application_Icon.jpg");
|
||||
|
||||
/**
|
||||
* Charge une image à partir du fichier spécifié.
|
||||
*
|
||||
* @param filePath Chemin du fichier image à charger.
|
||||
* @return L'image chargée, ou null si une erreur se produit.
|
||||
*/
|
||||
public static Image loadImage(String filePath) {
|
||||
try {
|
||||
File imageFile = new File(filePath);
|
||||
return ImageIO.read(imageFile);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Erreur lors du chargement de l'image : " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
94
TestV1/src/fr/monkhanny/dorfromantik/utils/MusicPlayer.java
Normal file
94
TestV1/src/fr/monkhanny/dorfromantik/utils/MusicPlayer.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Musics;
|
||||
import fr.monkhanny.dorfromantik.enums.Sounds;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
|
||||
public class MusicPlayer {
|
||||
private static Clip musicClip;
|
||||
private static Clip soundClip; // Clip séparé pour les bruitages
|
||||
private static boolean isPlayingMusic = false;
|
||||
private static boolean isPlayingSound = false;
|
||||
|
||||
public static void loadMusic(Musics music) {
|
||||
if (music == Musics.MAIN_MENU_MUSIC) {
|
||||
musicClip = SoundLoader.loadMusic(Musics.MAIN_MENU_MUSIC.getSoundsPath());
|
||||
if (musicClip != null) {
|
||||
setVolume(musicClip, Options.MUSIC_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadSound(Sounds sound) {
|
||||
if (sound == Sounds.SOUNDS1) {
|
||||
soundClip = SoundLoader.loadMusic(Sounds.SOUNDS1.getSoundsPath()); // Utilise soundClip pour les bruitages
|
||||
if (soundClip != null) {
|
||||
setVolume(soundClip, Options.SOUNDS_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void playMusic() {
|
||||
if (musicClip != null && !isPlayingMusic && !Options.MUSIC_MUTED) {
|
||||
musicClip.start();
|
||||
isPlayingMusic = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void playSound() {
|
||||
if (soundClip != null && !isPlayingSound && !Options.SOUNDS_MUTED) {
|
||||
soundClip.start();
|
||||
isPlayingSound = true;
|
||||
soundClip.addLineListener(event -> { // Réinitialiser isPlayingSound à la fin du son
|
||||
if (event.getType() == LineEvent.Type.STOP) {
|
||||
soundClip.setFramePosition(0); // Retour au début du son pour rejouer si nécessaire
|
||||
isPlayingSound = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void pauseMusic() {
|
||||
if (musicClip != null && isPlayingMusic) {
|
||||
musicClip.stop();
|
||||
isPlayingMusic = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void stopMusic() {
|
||||
if (musicClip != null) {
|
||||
musicClip.stop();
|
||||
musicClip.setFramePosition(0);
|
||||
isPlayingMusic = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setVolume(Clip clip, int volume) {
|
||||
if (clip != null) {
|
||||
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
|
||||
float range = volumeControl.getMaximum() - volumeControl.getMinimum();
|
||||
float gain = (range * volume / 100f) + volumeControl.getMinimum();
|
||||
volumeControl.setValue(gain);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPlayingMusic() {
|
||||
return isPlayingMusic;
|
||||
}
|
||||
|
||||
public static boolean isPlayingSound() {
|
||||
return isPlayingSound;
|
||||
}
|
||||
|
||||
public static Clip getMusicClip() {
|
||||
return musicClip;
|
||||
}
|
||||
|
||||
public static Clip getSoundClip() {
|
||||
return soundClip;
|
||||
}
|
||||
}
|
23
TestV1/src/fr/monkhanny/dorfromantik/utils/SoundLoader.java
Normal file
23
TestV1/src/fr/monkhanny/dorfromantik/utils/SoundLoader.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SoundLoader {
|
||||
|
||||
public static Clip loadMusic(String filePath) {
|
||||
try {
|
||||
File soundFile = new File(filePath);
|
||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundFile);
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(audioStream);
|
||||
return clip;
|
||||
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Failed to load sound at path: " + filePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user