Ajout d'un chartPanel + modifications de la BDD

This commit is contained in:
2024-12-03 13:09:04 +01:00
parent 85613b5286
commit 96a02dd5cb
3 changed files with 45291 additions and 45277 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,72 @@
package fr.monkhanny.dorfromantik.game;
import java.awt.*;
import java.util.List;
import javax.swing.*;
public class BarChartPanel extends JPanel {
private List<Integer> groupAverages;
private int highlightedGroup;
private JPanel mainPanel; // Store reference to mainPanel
public BarChartPanel(List<Integer> groupAverages, int highlightedGroup, JPanel mainPanel) {
this.groupAverages = groupAverages;
this.highlightedGroup = highlightedGroup;
this.mainPanel = mainPanel;
// Rendre le fond transparent et ajouter une bordure noire
setBackground(new Color(0, 0, 0, 0)); // Fond transparent
setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int barWidth = width / (groupAverages.size() + 1); // Espacement entre les groupes
int maxScore = groupAverages.stream().max(Integer::compare).orElse(0);
// Dessiner les barres et leurs étiquettes (moyennes des scores)
for (int i = 0; i < groupAverages.size(); i++) {
int barHeight = (int) ((double) groupAverages.get(i) / maxScore * (height - 50)); // Ajuster la hauteur des barres
int xPosition = (i + 1) * barWidth; // Espacement entre les groupes
// Appliquer une couleur de barre (mettre en évidence le groupe du joueur en rouge, les autres en bleu)
if (i == highlightedGroup) {
g.setColor(new Color(255, 69, 0)); // Couleur rouge pour le groupe du joueur
} else {
g.setColor(new Color(70, 130, 180)); // Bleu pour les autres groupes
}
// Ajouter des ombres à la barre pour un effet 3D
g.fillRect(xPosition, height - barHeight - 30, barWidth - 5, barHeight);
g.setColor(new Color(0, 0, 0, 60)); // Ombre
g.fillRect(xPosition + 2, height - barHeight - 28, barWidth - 5, barHeight);
// Dessiner le texte (moyenne) au-dessus de chaque barre
String avgScoreText = String.valueOf(groupAverages.get(i));
FontMetrics metrics = g.getFontMetrics();
int textWidth = metrics.stringWidth(avgScoreText);
int textX = xPosition + (barWidth - textWidth) / 2; // Centrer le texte
int textY = height - barHeight - 35; // Placer le texte juste au-dessus de la barre
g.setColor(Color.BLACK); // Couleur du texte
g.drawString(avgScoreText, textX, textY);
// Ajouter un label pour préciser que c'est la "moyenne"
g.setFont(new Font("Arial", Font.ITALIC, 12)); // Police italique plus petite pour le label "Moyenne"
String label = "Score moyen : ";
int labelWidth = metrics.stringWidth(label);
g.drawString(label, xPosition + (barWidth - labelWidth) / 2, textY - 15); // Placer "Moyenne" au-dessus du score
// Dessiner l'étiquette de groupe (Groupe 1, Groupe 2, ...)
String groupLabel = "Groupe " + (i + 1);
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.PLAIN, 14)); // Police plus petite pour les étiquettes
int groupLabelWidth = g.getFontMetrics().stringWidth(groupLabel);
g.drawString(groupLabel, xPosition + (barWidth - groupLabelWidth) / 2, height - 10); // Positionner l'étiquette sous la barre
}
}
}

View File

@@ -12,6 +12,7 @@ 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;
@@ -21,6 +22,7 @@ public class GameOver extends JPanel {
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;
@@ -67,42 +69,33 @@ public class GameOver extends JPanel {
database.addScore(seriesId, finalScore);
List<Integer> allScores = database.getScoresBySeriesId(seriesId);
// Calculate the groups
// Check if there are enough players to form groups
int totalPlayers = allScores.size();
int groupSize = totalPlayers / 10;
int playerGroup = 0;
if (totalPlayers >= 100) {
int groupSize = totalPlayers / 10;
int playerGroup = 0;
// Check if there are less than 20 players
String funnyQuote = "";
if (totalPlayers < 20) {
// Only show the funny quote if there are less than 20 players
if(totalPlayers == 0){
funnyQuote = "Vous êtes le premier joueur à jouer à cette partie personalisée...";
}
if(totalPlayers == 1){
funnyQuote = "Vous êtes le deuxième joueur à jouer à cette partie personalisée...";
}
if(totalPlayers > 1){
funnyQuote = "À part vous, il y a " + totalPlayers + " joueurs qui à joué à cette partie personalisée...";
}
// Display the funny quote directly
JLabel quoteLabel = new JLabel(funnyQuote);
quoteLabel.setFont(Fonts.SCORE.getFont(24));
quoteLabel.setForeground(Color.WHITE);
quoteLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(quoteLabel);
} else {
// Calculate which group the player's score falls into
for (int i = 0; i < totalPlayers; i++) {
if (allScores.get(i) <= finalScore) {
playerGroup = i / groupSize + 1;
break;
// Group data for bar chart
List<Integer> 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<Integer> 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
// Group information panel
JPanel groupPanel = new JPanel();
groupPanel.setOpaque(false);
groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS));
@@ -113,14 +106,21 @@ public class GameOver extends JPanel {
groupTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
groupPanel.add(groupTitleLabel);
JLabel groupSizeLabel = new JLabel("Il y a " + groupSize + " joueurs dans ce groupe.");
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
funnyQuote = getFunnyQuote(playerGroup);
String funnyQuote = getFunnyQuote(playerGroup);
JLabel quoteLabel = new JLabel(funnyQuote);
quoteLabel.setFont(Fonts.SCORE.getFont(24));
quoteLabel.setForeground(Color.WHITE);
@@ -129,9 +129,13 @@ public class GameOver extends JPanel {
// Add group information panel
mainPanel.add(groupPanel);
// Spacer
mainPanel.add(Box.createVerticalStrut(30));
} 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) {
@@ -140,7 +144,7 @@ public class GameOver extends JPanel {
// 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));
@@ -148,17 +152,24 @@ public class GameOver extends JPanel {
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<Integer> 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
@@ -176,5 +187,5 @@ public class GameOver extends JPanel {
default: return "Hé, on progresse ! Peut-être qu'un jour, vous dominerez le monde... ou du moins ce jeu !";
}
}
}
}