185 lines
7.4 KiB
Java
185 lines
7.4 KiB
Java
|
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;
|
||
|
}
|
||
|
}
|