Ajout de récompenses

This commit is contained in:
2024-11-24 13:16:49 +01:00
parent e0bd133e38
commit 13cbfa3572
15 changed files with 45540 additions and 39 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

View File

@@ -1,6 +1,7 @@
package fr.monkhanny.dorfromantik; package fr.monkhanny.dorfromantik;
import fr.monkhanny.dorfromantik.gui.MainMenu; import fr.monkhanny.dorfromantik.gui.MainMenu;
import fr.monkhanny.dorfromantik.gui.RewardsPanel;
import fr.monkhanny.dorfromantik.controller.MainMenuResizeController; import fr.monkhanny.dorfromantik.controller.MainMenuResizeController;
import fr.monkhanny.dorfromantik.controller.MainMenuButtonController; import fr.monkhanny.dorfromantik.controller.MainMenuButtonController;
import fr.monkhanny.dorfromantik.utils.MusicPlayer; import fr.monkhanny.dorfromantik.utils.MusicPlayer;
@@ -39,6 +40,9 @@ public class Main {
// Créer la fenêtre du tutoriel // Créer la fenêtre du tutoriel
JFrame howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik"); JFrame howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik");
// Créer la fenêtre des récompenses
JFrame rewardsFrame = new JFrame("Récompenses - Dorfromantik");
// Menu principal // Menu principal
MusicPlayer.loadMusic(Musics.MAIN_MENU_MUSIC); MusicPlayer.loadMusic(Musics.MAIN_MENU_MUSIC);
MusicPlayer.playMusic(); MusicPlayer.playMusic();
@@ -46,7 +50,7 @@ public class Main {
@SuppressWarnings("unused") @SuppressWarnings("unused")
MainMenuResizeController MainMenuResizeController = new MainMenuResizeController(mainMenu); MainMenuResizeController MainMenuResizeController = new MainMenuResizeController(mainMenu);
@SuppressWarnings("unused") @SuppressWarnings("unused")
MainMenuButtonController MainMenuButtonController = new MainMenuButtonController(mainMenu,settingsFrame,howToPlayFrame,gameModeFrame,gameFrame); MainMenuButtonController MainMenuButtonController = new MainMenuButtonController(mainMenu,settingsFrame,howToPlayFrame,gameModeFrame,gameFrame, rewardsFrame);
// Fenêtre des paramètres // Fenêtre des paramètres
@@ -69,5 +73,12 @@ public class Main {
gameModeController.setGameModeSelectionPanel(gameModeSelectionPanel); gameModeController.setGameModeSelectionPanel(gameModeSelectionPanel);
gameModeFrame.add(gameModeSelectionPanel); gameModeFrame.add(gameModeSelectionPanel);
// Fenêtre des récompenses
CloseWindowListener rewardsWindowListener = new CloseWindowListener(mainMenu, rewardsFrame);
rewardsFrame.addWindowListener(rewardsWindowListener);
RewardsPanel rewardsPanel = new RewardsPanel();
rewardsFrame.setContentPane(rewardsPanel);
rewardsFrame.pack();
} }
} }

View File

@@ -1,7 +1,7 @@
package fr.monkhanny.dorfromantik.controller; package fr.monkhanny.dorfromantik.controller;
import fr.monkhanny.dorfromantik.Options;
import fr.monkhanny.dorfromantik.gui.MainMenu; import fr.monkhanny.dorfromantik.gui.MainMenu;
import fr.monkhanny.dorfromantik.gui.RewardsPanel;
import fr.monkhanny.dorfromantik.gui.ButtonPanel; import fr.monkhanny.dorfromantik.gui.ButtonPanel;
import javax.swing.*; import javax.swing.*;
@@ -10,6 +10,8 @@ import java.awt.event.ActionListener;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Point; import java.awt.Point;
public class MainMenuButtonController implements ActionListener { public class MainMenuButtonController implements ActionListener {
private MainMenu mainMenu; private MainMenu mainMenu;
@@ -18,8 +20,9 @@ public class MainMenuButtonController implements ActionListener {
private JFrame howToPlayFrame; private JFrame howToPlayFrame;
private JFrame gameModeFrame; private JFrame gameModeFrame;
private JFrame gameFrame; private JFrame gameFrame;
private JFrame rewardsFrame;
public MainMenuButtonController(MainMenu mainMenu, JFrame settingsFrame, JFrame howToPlayFrame, JFrame gameModeFrame, JFrame gameFrame) { public MainMenuButtonController(MainMenu mainMenu, JFrame settingsFrame, JFrame howToPlayFrame, JFrame gameModeFrame, JFrame gameFrame, JFrame rewardsFrame) {
this.mainMenu = mainMenu; this.mainMenu = mainMenu;
// Ajouter les écouteurs d'événements sur les boutons // Ajouter les écouteurs d'événements sur les boutons
ButtonPanel buttonPanel = mainMenu.getButtonPanel(); ButtonPanel buttonPanel = mainMenu.getButtonPanel();
@@ -39,16 +42,17 @@ public class MainMenuButtonController implements ActionListener {
this.howToPlayFrame = howToPlayFrame; this.howToPlayFrame = howToPlayFrame;
configureFrame(this.howToPlayFrame); configureFrame(this.howToPlayFrame);
// Paramètrage de la fenêtre du jeu // Paramètrage de la fenêtre du jeu
this.gameModeFrame = gameModeFrame; this.gameModeFrame = gameModeFrame;
configureFrame(this.gameModeFrame); configureFrame(this.gameModeFrame);
// Paramètrage de la fenêtre du jeu // Paramètrage de la fenêtre du jeu
this.gameFrame = gameFrame; this.gameFrame = gameFrame;
configureFrame(this.gameFrame); configureFrame(this.gameFrame);
// Paramètrage de la fenêtre des récompenses
this.rewardsFrame = rewardsFrame;
configureFrame(this.rewardsFrame);
} }
private void configureFrame(JFrame frame) { private void configureFrame(JFrame frame) {
@@ -91,8 +95,8 @@ public class MainMenuButtonController implements ActionListener {
} }
private void openRecompense() { private void openRecompense() {
System.out.println("Récompenses..."); adjustFrameDisplay(this.rewardsFrame);
// Logic to continue the game this.rewardsFrame.setVisible(true);
} }
public void showHowToPlay() { public void showHowToPlay() {
@@ -114,16 +118,15 @@ public class MainMenuButtonController implements ActionListener {
private void adjustFrameDisplay(JFrame frame) { private void adjustFrameDisplay(JFrame frame) {
if (Options.FULL_SCREEN) { boolean wasVisible = frame.isVisible(); // Vérifier si la fenêtre était déjà visible
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Mettre la fenêtre en plein écran
frame.setUndecorated(true); // Retirer la décoration de la fenêtre if(!wasVisible){
} else {
// Récupérer la taille et la position de la fenêtre du menu principal // Récupérer la taille et la position de la fenêtre du menu principal
Dimension mainMenuSize = this.mainMenu.getSize(); Dimension mainMenuSize = this.mainMenu.getSize();
Point mainMenuLocation = this.mainMenu.getLocation(); Point mainMenuLocation = this.mainMenu.getLocation();
frame.setSize(mainMenuSize); frame.setSize(mainMenuSize);
frame.setLocation(mainMenuLocation); frame.setLocation(mainMenuLocation);
frame.setUndecorated(false); // Restaurer la décoration si nécessaire
} }
} }
} }

View File

@@ -0,0 +1,34 @@
package fr.monkhanny.dorfromantik.gui;
import javax.swing.*;
// Classe pour représenter une récompense
public class Reward {
private String name;
private String description;
private boolean isUnlocked;
private ImageIcon icon;
public Reward(String name, String description, boolean isUnlocked, ImageIcon icon) {
this.name = name;
this.description = description;
this.isUnlocked = isUnlocked;
this.icon = icon;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public boolean isUnlocked() {
return isUnlocked;
}
public ImageIcon getIcon() {
return icon;
}
}

View File

@@ -0,0 +1,184 @@
package fr.monkhanny.dorfromantik.gui;
import fr.monkhanny.dorfromantik.utils.Database;
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class RewardsPanel extends JPanel {
private JTextField usernameField;
private JPanel rewardsDisplayPanel;
public RewardsPanel() {
setLayout(new BorderLayout());
// Add background image
JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
background.setLayout(new GridBagLayout());
this.setLayout(new BorderLayout());
this.add(background);
// Main content panel
JPanel mainPanel = createMainPanel();
background.add(mainPanel);
// Username input panel
JPanel inputPanel = createInputPanel();
mainPanel.add(inputPanel, createGridBagConstraints(0, 0, 1));
// Rewards display panel
rewardsDisplayPanel = new JPanel(new GridLayout(0, 3, 10, 10));
JScrollPane scrollPane = new JScrollPane(rewardsDisplayPanel);
mainPanel.add(scrollPane, createGridBagConstraints(0, 1, 1));
// Fetch button action
JButton fetchButton = new JButton("Afficher les récompenses de l'utilisateur");
fetchButton.setFont(new Font("Arial", Font.BOLD, 20));
fetchButton.setBackground(new Color(0, 122, 255));
fetchButton.setForeground(Color.WHITE);
fetchButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
fetchButton.setPreferredSize(new Dimension(500, 50));
fetchButton.addActionListener(e -> {
String username = usernameField.getText().trim();
if (!username.isEmpty()) {
try {
// Fetch rewards for the entered username
Database db = new Database();
List<Reward> rewards = db.getRewardsByUsername(username);
db.close();
// Update the panel
updateRewardsPanel(rewards);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error fetching rewards: " + ex.getMessage());
}
}
});
// Add the button to the bottom
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
buttonPanel.add(fetchButton);
mainPanel.add(buttonPanel, createGridBagConstraints(0, 2, 1));
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setOpaque(false);
return mainPanel;
}
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 createInputPanel() {
JPanel inputPanel = new JPanel();
inputPanel.setOpaque(false);
inputPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
JLabel usernameLabel = new JLabel("Entrer le nom d'utilisateur :");
usernameLabel.setForeground(Color.WHITE);
inputPanel.add(usernameLabel);
usernameField = new JTextField(20);
usernameField.setFont(new Font("Arial", Font.PLAIN, 18));
usernameField.setPreferredSize(new Dimension(250, 40));
usernameField.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
inputPanel.add(usernameField);
return inputPanel;
}
private void updateRewardsPanel(List<Reward> rewards) {
rewardsDisplayPanel.removeAll(); // Clear the previous contents
// Update the layout of the rewards display
rewardsDisplayPanel.setLayout(new GridLayout(0, 3, 10, 10)); // 3 columns grid
// Add the new rewards to the panel
for (Reward reward : rewards) {
JPanel rewardPanel = createRewardPanel(reward);
rewardsDisplayPanel.add(rewardPanel);
}
// Force the container to revalidate and repaint
rewardsDisplayPanel.revalidate();
rewardsDisplayPanel.repaint();
// Optionally force the parent panel to refresh as well:
this.revalidate();
this.repaint();
}
private JPanel createRewardPanel(Reward reward) {
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(180, 220)); // Agrandir légèrement la taille du panneau
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
panel.setBackground(reward.isUnlocked() ? new Color(245, 245, 245) : new Color(230, 230, 230)); // Couleur de fond plus douce
// Effet de survol : changement de couleur de fond
panel.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
panel.setBackground(reward.isUnlocked() ? new Color(240, 240, 240) : new Color(210, 210, 210));
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
panel.setBackground(reward.isUnlocked() ? new Color(245, 245, 245) : new Color(230, 230, 230));
}
});
panel.setLayout(new BorderLayout());
panel.setBackground(reward.isUnlocked() ? new Color(255, 255, 255) : new Color(230, 230, 230));
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(180, 180, 180), 2),
BorderFactory.createEmptyBorder(10, 10, 10, 10) // Ajoute de l'espace autour du contenu
));
// Ajouter une ombre portée
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.GRAY, 1),
BorderFactory.createEmptyBorder(5, 5, 5, 5)
));
// Icône
JLabel iconLabel = new JLabel();
if (reward.getIcon() != null) {
iconLabel.setIcon(reward.getIcon());
} else {
iconLabel.setText("No Icon");
}
iconLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(iconLabel, BorderLayout.CENTER);
// Titre (nom de la récompense)
JLabel nameLabel = new JLabel(reward.getName());
nameLabel.setHorizontalAlignment(JLabel.CENTER);
nameLabel.setFont(new Font("Arial", Font.BOLD, 16));
nameLabel.setForeground(new Color(50, 50, 50)); // Change la couleur du texte
nameLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Ajoute un peu d'espace autour du titre
panel.add(nameLabel, BorderLayout.NORTH);
// Description de la récompense
JLabel descriptionLabel = new JLabel("<html><body style='text-align: center;'>" + reward.getDescription() + "</body></html>");
descriptionLabel.setHorizontalAlignment(JLabel.CENTER);
descriptionLabel.setFont(new Font("Arial", Font.PLAIN, 12));
descriptionLabel.setForeground(new Color(100, 100, 100)); // Une couleur plus douce pour le texte de description
panel.add(descriptionLabel, BorderLayout.SOUTH);
// Coins arrondis
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2, true)); // Utilisation de bordures arrondies
return panel;
}
}

View File

@@ -11,6 +11,10 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.swing.ImageIcon;
import fr.monkhanny.dorfromantik.gui.Reward;
public class Database { public class Database {
// Chargement des variables d'environnement // Chargement des variables d'environnement
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/stiti"; private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/stiti";
@@ -191,6 +195,39 @@ public class Database {
return scores; return scores;
} }
/**
* Récupère les récompenses d'un utilisateur spécifique
* @param username Le nom d'utilisateur pour lequel récupérer les récompenses
* @return Liste des récompenses de l'utilisateur
* @throws SQLException En cas d'erreur lors de la récupération des récompenses
*/
public List<Reward> getRewardsByUsername(String username) throws SQLException {
List<Reward> rewards = new ArrayList<>();
String query = "SELECT r.name, r.description, r.icon_path, ur.is_unlocked " +
"FROM UserRewards ur " +
"JOIN Rewards r ON ur.reward_id = r.reward_id " +
"WHERE ur.username = ?";
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
stmt.setString(1, username);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String name = rs.getString("name");
String description = rs.getString("description");
String iconPath = rs.getString("icon_path");
boolean isUnlocked = rs.getBoolean("is_unlocked");
ImageIcon icon = (iconPath != null && !iconPath.isEmpty()) ? new ImageIcon(iconPath) : null;
Reward reward = new Reward(name, description, isUnlocked, icon);
rewards.add(reward);
}
}
}
return rewards;
}
public void close() { public void close() {
try { try {
if (this.database != null && !this.database.isClosed()) { if (this.database != null && !this.database.isClosed()) {