package fr.monkhanny.dorfromantik.gui; import java.awt.*; import java.util.List; import javax.swing.*; public class BarChartPanel extends JPanel { private List groupAverages; private int highlightedGroup; public BarChartPanel(List groupAverages, int highlightedGroup, JPanel mainPanel) { this.groupAverages = groupAverages; this.highlightedGroup = highlightedGroup; // 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(204, 0, 0)); // Couleur rouge pour le groupe du joueur } else { g.setColor(new Color(0, 0, 204)); // 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 } } }