Files
SAE31_2024/TestV2/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java

182 lines
6.9 KiB
Java

package fr.monkhanny.dorfromantik.gui;
import fr.monkhanny.dorfromantik.utils.Database;
import fr.monkhanny.dorfromantik.components.Title;
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());
// Ajouter le fond d'écran
JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
background.setLayout(new GridBagLayout());
this.setLayout(new BorderLayout());
this.add(background);
// Titre du panneau
JPanel titlePanel = createTitlePanel();
background.add(titlePanel, createGridBagConstraints(0, 0, 1));
// Panel principal
JPanel mainPanel = createMainPanel();
background.add(mainPanel, createGridBagConstraints(0, 1, 1));
// Panel d'entrée (nom d'utilisateur)
JPanel inputPanel = createInputPanel();
mainPanel.add(inputPanel, createGridBagConstraints(0, 0, 1));
// Panel pour afficher les récompenses
rewardsDisplayPanel = new JPanel(new GridLayout(0, 3, 10, 10));
JScrollPane scrollPane = new JScrollPane(rewardsDisplayPanel);
mainPanel.add(scrollPane, createGridBagConstraints(0, 1, 1));
// Action du bouton pour afficher les récompenses
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 {
// Récupérer les récompenses pour l'utilisateur
Database db = new Database();
List<Reward> rewards = db.getRewardsByUsername(username);
db.close();
// Mettre à jour le panneau
updateRewardsPanel(rewards);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error fetching rewards: " + ex.getMessage());
}
}
});
// Ajouter le bouton en bas
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
buttonPanel.add(fetchButton);
mainPanel.add(buttonPanel, createGridBagConstraints(0, 2, 1));
}
private JPanel createTitlePanel() {
JPanel titlePanel = new JPanel(new BorderLayout());
titlePanel.setOpaque(false);
// Création du titre
Title title = new Title("Récompenses", 70, Color.WHITE);
title.setHorizontalAlignment(JLabel.CENTER); // Centrer le titre
titlePanel.add(title, BorderLayout.CENTER);
return titlePanel;
}
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);
usernameLabel.setFont(new Font("Arial", Font.BOLD, 20));
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 previous contents
if (rewards.isEmpty()) {
// If no rewards, show a message
JLabel noRewardsLabel = new JLabel("Aucune récompense trouvée...", JLabel.CENTER);
noRewardsLabel.setFont(new Font("Arial", Font.BOLD, 18));
noRewardsLabel.setForeground(Color.RED);
rewardsDisplayPanel.add(noRewardsLabel);
} else {
// Otherwise, display the rewards
rewardsDisplayPanel.setLayout(new GridLayout(0, 3, 10, 10));
// Add each reward to the panel
for (Reward reward : rewards) {
JPanel rewardPanel = createRewardPanel(reward);
rewardsDisplayPanel.add(rewardPanel);
}
}
rewardsDisplayPanel.revalidate();
rewardsDisplayPanel.repaint();
this.revalidate();
this.repaint();
}
private JPanel createRewardPanel(Reward reward) {
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(180, 220));
Color backgroundColor = reward.isUnlocked() ? new Color(230, 255, 230) : new Color(255, 245, 235);
panel.setBackground(backgroundColor);
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(150, 200, 150), 1),
BorderFactory.createEmptyBorder(10, 10, 10, 10)
));
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);
JLabel nameLabel = new JLabel(reward.getName());
nameLabel.setHorizontalAlignment(JLabel.CENTER);
nameLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
nameLabel.setForeground(new Color(80, 120, 80));
nameLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.add(nameLabel, BorderLayout.NORTH);
JLabel descriptionLabel = new JLabel("<html><body style='text-align: center;'>" + reward.getDescription() + "</body></html>");
descriptionLabel.setHorizontalAlignment(JLabel.CENTER);
descriptionLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12));
descriptionLabel.setForeground(new Color(90, 90, 90));
panel.add(descriptionLabel, BorderLayout.SOUTH);
return panel;
}
}