package fr.monkhanny.dorfromantik.game; import fr.monkhanny.dorfromantik.utils.Database; import fr.monkhanny.dorfromantik.gui.MainMenu; import fr.monkhanny.dorfromantik.Main; import fr.monkhanny.dorfromantik.Options; import fr.monkhanny.dorfromantik.enums.Fonts; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import java.util.ArrayList; public class GameOver extends JPanel { private JFrame gameFrame; private int finalScore; private Database database; private MainMenu mainMenu; public GameOver(JFrame gameFrame, int finalScore, Database database, MainMenu mainMenu) { this.gameFrame = gameFrame; this.gameFrame.setTitle("Partie terminée - Dorfromantik"); this.mainMenu = mainMenu; this.finalScore = finalScore; this.database = database; setLayout(new BorderLayout()); // Background image setup JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg")); background.setLayout(new BorderLayout()); this.add(background, BorderLayout.CENTER); // Main content panel JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.setOpaque(false); background.add(mainPanel, BorderLayout.CENTER); // Title for the Game Over message JLabel titleLabel = new JLabel("Partie terminée !"); titleLabel.setFont(Fonts.TITLE.getFont(48)); // Using the TITLE font titleLabel.setForeground(Color.WHITE); titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); mainPanel.add(titleLabel); // Spacer mainPanel.add(Box.createVerticalStrut(30)); // Display the final score JLabel scoreLabel = new JLabel("Votre score est de : " + finalScore); scoreLabel.setFont(Fonts.SCORE.getFont(36)); // Using the SCORE font scoreLabel.setForeground(Color.WHITE); scoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT); scoreLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); // Padding scoreLabel.setOpaque(true); scoreLabel.setBackground(new Color(0, 0, 0, 100)); // Background color with transparency mainPanel.add(scoreLabel); // Spacer mainPanel.add(Box.createVerticalStrut(30)); // Grouping information and funny quote try { long seriesId = Options.SEED; // Get the correct seriesId database.addScore(seriesId, finalScore); List allScores = database.getScoresBySeriesId(seriesId); // Check if there are enough players to form groups int totalPlayers = allScores.size(); if (totalPlayers >= 100) { int groupSize = totalPlayers / 10; int playerGroup = 0; // Group data for bar chart List groupAverages = new ArrayList<>(); // Calculate which group the player's score falls into and the average score for each group for (int i = 0; i < 10; i++) { int startIdx = i * groupSize; int endIdx = Math.min((i + 1) * groupSize, totalPlayers); if (startIdx < totalPlayers) { List groupScores = allScores.subList(startIdx, endIdx); int groupSum = 0; for (int score : groupScores) { groupSum += score; } groupAverages.add(groupSum / groupScores.size()); if (scoreInGroup(allScores, finalScore, startIdx, endIdx)) { playerGroup = i + 1; } } } // Group information panel JPanel groupPanel = new JPanel(); groupPanel.setOpaque(false); groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS)); JLabel groupTitleLabel = new JLabel("Vous êtes dans le groupe " + playerGroup + "/10 !"); groupTitleLabel.setFont(Fonts.SCORE.getFont(24)); groupTitleLabel.setForeground(Color.WHITE); groupTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); groupPanel.add(groupTitleLabel); BarChartPanel barChartPanel = new BarChartPanel(groupAverages, playerGroup - 1, mainPanel); barChartPanel.setPreferredSize(new Dimension(700, 400)); barChartPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement groupPanel.add(barChartPanel); groupPanel.add(Box.createVerticalStrut(30)); JLabel groupSizeLabel = new JLabel("Il y a " + groupSize + " joueurs dans votre groupe."); groupSizeLabel.setFont(Fonts.SCORE.getFont(24)); groupSizeLabel.setForeground(Color.WHITE); groupSizeLabel.setAlignmentX(Component.CENTER_ALIGNMENT); groupPanel.add(groupSizeLabel); // Show a funny quote based on the group String funnyQuote = getFunnyQuote(playerGroup); JLabel quoteLabel = new JLabel(funnyQuote); quoteLabel.setFont(Fonts.SCORE.getFont(24)); quoteLabel.setForeground(Color.WHITE); quoteLabel.setAlignmentX(Component.CENTER_ALIGNMENT); groupPanel.add(quoteLabel); // Add group information panel mainPanel.add(groupPanel); } else { // No groups, display a simple message instead JLabel noGroupsLabel = new JLabel("Pas mal !"); noGroupsLabel.setFont(Fonts.SCORE.getFont(24)); noGroupsLabel.setForeground(Color.WHITE); noGroupsLabel.setAlignmentX(Component.CENTER_ALIGNMENT); mainPanel.add(noGroupsLabel); } } catch (SQLException e) { e.printStackTrace(); } // Spacer mainPanel.add(Box.createVerticalStrut(30)); // Bouton pour retourner au menu principal JButton returnButton = new JButton("Retour au Menu Principal"); returnButton.setFont(Fonts.BUTTON.getFont(24)); returnButton.setAlignmentX(Component.CENTER_ALIGNMENT); returnButton.setFocusPainted(false); // Optionnel : pour un style plus propre returnButton.setBackground(new Color(0, 0, 0)); // Couleur d'arrière-plan du bouton returnButton.setForeground(Color.BLACK); // Couleur du texte du bouton // Ajouter un listener d'action au bouton MainMenuButtonListener listener = new MainMenuButtonListener(gameFrame, null, null); returnButton.addActionListener(listener); // Ajouter le bouton au panneau principal mainPanel.add(returnButton); } private boolean scoreInGroup(List allScores, int finalScore, int startIdx, int endIdx) { for (int i = startIdx; i < endIdx; i++) { if (allScores.get(i) == finalScore) { return true; } } return false; } private String getFunnyQuote(int playerGroup) { // A list of funny and motivational quotes based on the group switch (playerGroup) { case 1: return "Vous êtes officiellement un génie ! Peut-être même un super-héros...!"; case 2: return "Pas mal ! Mais attention, le groupe 1 vous attend avec des applaudissements !"; case 3: return "Vous êtes sur la bonne voie, mais vous avez encore un peu de chemin à parcourir !"; case 4: return "Il est encore temps d'appeler un coach... ou un ami pour vous aider !"; case 5: return "Vous êtes dans la bonne direction, mais votre GPS semble un peu perdu !"; case 6: return "Vous n'êtes pas loin du sommet, mais le sommet semble être dans un autre pays !"; case 7: return "C'est un bon début ! Peut-être qu'un peu de café améliorerait encore la situation ?!"; case 8: return "Sur le chemin de la gloire, mais vous êtes encore coincé dans les bouchons..."; case 9: return "Pas de panique, il y a encore de la place pour s'améliorer... à peu près toute la place."; case 10: return "Félicitations ! Mais peut-être que vous voudriez réessayer sans les lunettes de soleil ?"; default: return "Hé, on progresse ! Peut-être qu'un jour, vous dominerez le monde... ou du moins ce jeu !"; } } }