Ajouts d'une version BETA du menu principal qui respecte le modèle MVC et la responsabilité unique + Modifications du README.md

This commit is contained in:
2024-11-06 14:26:46 +01:00
parent eebb8259b0
commit d0c67d5298
28 changed files with 422 additions and 2 deletions

View File

@@ -1,3 +1,3 @@
# 🌄 Dorfromantik 🌄
# Dorfromantik 🌄
À VENIR !
À VENIR !

41
TestV2/Makefile Normal file
View File

@@ -0,0 +1,41 @@
PACKAGE = fr.monkhanny.dorfromantik
ENTRY = Main
SOURCEDIR = ./src/fr/monkhanny/dorfromantik/
BUILDDIR = ./build/
DOCDIR = ./doc/
JARNAME = dorfromantik.jar
CLASSP = libs/*
MANIFESTPATH = Manifest.MF
SOURCEDIR = ./src/
SOURCES := $(shell find $(SOURCEDIR) -name '*.java')
all:
@make compile
@make jar
@make run
compile:
@echo "Compiling..."
@javac -cp $(CLASSP) -d $(BUILDDIR) $(SOURCES) -Xlint:unchecked -Xlint:deprecation
@echo "Done."
run:
@echo "Running..."
@java -jar $(JARNAME)
@echo "Done."
clean:
@echo "Cleaning up..."
@rm -rf $(BUILDDIR)* $(DOCDIR)*
@echo "Done."
javadoc:
@echo "Generating javadoc..."
@javadoc -d $(DOCDIR) -sourcepath src -subpackages $(PACKAGE)
@echo "Done."
jar:
@echo "Creating jar..."
@jar cfm $(JARNAME) $(MANIFESTPATH) -C $(BUILDDIR) fr/monkhanny/dorfromantik ressources
@echo "Done."

3
TestV2/Manifest.MF Normal file
View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: fr.monkhanny.dorfromantik.Main
Class-Path: libs/mariadb-client.jar

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

View File

@@ -0,0 +1,22 @@
package fr.monkhanny.dorfromantik;
import fr.monkhanny.dorfromantik.gui.MainMenu;
import fr.monkhanny.dorfromantik.controller.MainMenuController;
/**
* Classe principale du jeu
* @version 1.0
* @author Moncef STITI
* @see MainMenu
* @see MainMenuController
*/
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) {
MainMenu mainMenu = new MainMenu();
MainMenuController mainMenuController = new MainMenuController(mainMenu);
}
}

View File

@@ -0,0 +1,7 @@
package fr.monkhanny.dorfromantik;
public class Options {
public static final float BASE_TITLE_FONT_SIZE = 70f;
public static final float BASE_BUTTON_FONT_SIZE = 50f;
}

View File

@@ -0,0 +1,19 @@
package fr.monkhanny.dorfromantik.components;
import fr.monkhanny.dorfromantik.utils.FontManager;
import javax.swing.*;
import java.awt.*;
public class Button {
public static JButton createStyledButton(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;
}
}

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

View File

@@ -0,0 +1,31 @@
package fr.monkhanny.dorfromantik.controller;
import fr.monkhanny.dorfromantik.gui.MainMenu;
import fr.monkhanny.dorfromantik.Options;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class MainMenuController {
private MainMenu mainMenu;
public MainMenuController(MainMenu mainMenu) {
this.mainMenu = mainMenu;
addComponentListener();
}
private void addComponentListener() {
mainMenu.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
// Ajuster la taille de la police du titre en fonction de la taille de la fenêtre
float newFontSize = Options.BASE_TITLE_FONT_SIZE * (mainMenu.getWidth() / 900f);
mainMenu.getTitleLabel().updateTitleFont(newFontSize);
// Mettre à jour les polices des boutons
mainMenu.getButtonPanel().updateButtonFonts(mainMenu.getWidth());
}
});
}
}

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

View File

@@ -0,0 +1,59 @@
package fr.monkhanny.dorfromantik.gui;
import fr.monkhanny.dorfromantik.utils.FontManager;
import fr.monkhanny.dorfromantik.components.Button;
import javax.swing.*;
import java.awt.*;
public class ButtonPanel extends JPanel {
private JButton newGameButton;
private JButton continueGameButton;
private JButton howToPlayButton;
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.createStyledButton("Nouvelle partie", fontSize);
continueGameButton = Button.createStyledButton("Continuer une partie", fontSize);
howToPlayButton = Button.createStyledButton("Comment jouer ?", fontSize);
// Ajouter les boutons au panneau
this.add(newGameButton);
this.add(Box.createVerticalStrut(20)); // Espace entre les boutons
this.add(continueGameButton);
this.add(Box.createVerticalStrut(20));
this.add(howToPlayButton);
// Espacement extensible pour maintenir les icônes en bas
this.add(Box.createVerticalGlue());
}
public JButton getNewGameButton() {
return newGameButton;
}
public JButton getContinueGameButton() {
return continueGameButton;
}
public JButton getHowToPlayButton() {
return howToPlayButton;
}
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));
}
}

View File

@@ -0,0 +1,55 @@
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.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;
}
}

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

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

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