ajout d'une icône d'application et amélioration du panneau BarChart avec des méthodes de dessin centrées et une gestion des barres plus efficace
This commit is contained in:
Binary file not shown.
Before Width: | Height: | Size: 16 KiB |
Binary file not shown.
Before Width: | Height: | Size: 8.2 KiB |
@@ -11,6 +11,7 @@ import fr.monkhanny.dorfromantik.controller.TutorialController;
|
||||
import fr.monkhanny.dorfromantik.controller.GameModeController;
|
||||
import fr.monkhanny.dorfromantik.gui.GameModeSelectionPanel;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
/**
|
||||
@@ -76,14 +77,21 @@ public class Main {
|
||||
isMusicPlayed = true; // Marquer la musique comme jouée
|
||||
}
|
||||
|
||||
|
||||
// 4. Créer les fenêtres à nouveau comme au début
|
||||
ImageIcon icon = new ImageIcon("./ressources/images/Application/Application_Icon.jpg");
|
||||
|
||||
gameModeFrame = new JFrame("Choix des séries - Dorfromantik");
|
||||
gameModeFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||
gameModeFrame.setIconImage(icon.getImage());
|
||||
gameFrame = new JFrame("Jeu - Dorfromantik");
|
||||
gameFrame.setIconImage(icon.getImage());
|
||||
gameFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||
settingsFrame = new JFrame("Paramètres - Dorfromantik");
|
||||
settingsFrame.setIconImage(icon.getImage());
|
||||
settingsFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||
howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik");
|
||||
howToPlayFrame.setIconImage(icon.getImage());
|
||||
howToPlayFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||
|
||||
// Re-créer et réinitialiser les panels et les contrôleurs
|
||||
|
@@ -2,69 +2,131 @@ package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.swing.*;
|
||||
|
||||
public class BarChartPanel extends JPanel {
|
||||
private List<Integer> groupAverages;
|
||||
private int highlightedGroup;
|
||||
private final List<Integer> groupAverages;
|
||||
private final int highlightedGroup;
|
||||
private static final Color PLAYER_GROUP_COLOR = new Color(204, 0, 0);
|
||||
private static final Color OTHER_GROUP_COLOR = new Color(0, 0, 204);
|
||||
private static final Color SHADOW_COLOR = new Color(0, 0, 0, 60);
|
||||
private static final Color TEXT_COLOR = Color.BLACK;
|
||||
private static final Font AVERAGE_FONT = new Font("Arial", Font.ITALIC, 12);
|
||||
private static final Font GROUP_LABEL_FONT = new Font("Arial", Font.BOLD, 16);
|
||||
private static final Font SCORE_FONT = new Font("Arial", Font.BOLD, 14);
|
||||
private static final int PADDING = 30;
|
||||
private static final int SHADOW_OFFSET = 2;
|
||||
|
||||
public BarChartPanel(List<Integer> groupAverages, int highlightedGroup, JPanel mainPanel) {
|
||||
this.groupAverages = groupAverages;
|
||||
public BarChartPanel(List<Integer> groupAverages, int highlightedGroup) {
|
||||
this.groupAverages = Objects.requireNonNull(groupAverages, "Group averages cannot be null");
|
||||
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
|
||||
// Configuration du panneau
|
||||
setOpaque(false);
|
||||
setBackground(new Color(0, 0, 0, 0));
|
||||
setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
if (groupAverages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g.create();
|
||||
try {
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
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);
|
||||
int barWidth = width / (groupAverages.size() + 1);
|
||||
int maxScore = groupAverages.stream()
|
||||
.mapToInt(Integer::intValue)
|
||||
.max()
|
||||
.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
|
||||
int barHeight = calculateBarHeight(maxScore, height, groupAverages.get(i));
|
||||
int xPosition = (i + 1) * barWidth;
|
||||
|
||||
// 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
|
||||
Color barColor = (i == highlightedGroup) ? PLAYER_GROUP_COLOR : OTHER_GROUP_COLOR;
|
||||
|
||||
g2d.setColor(SHADOW_COLOR);
|
||||
g2d.fillRect(xPosition + SHADOW_OFFSET, height - barHeight - PADDING + SHADOW_OFFSET, barWidth - 5, barHeight);
|
||||
|
||||
g2d.setColor(barColor);
|
||||
g2d.fillRect(xPosition, height - barHeight - PADDING, barWidth - 5, barHeight);
|
||||
|
||||
// Ajustez dynamiquement la position des textes
|
||||
int textY = height - barHeight - PADDING - 20; // Score position
|
||||
if (textY < PADDING) {
|
||||
textY = PADDING - 2; // Éviter le chevauchement au sommet
|
||||
}
|
||||
|
||||
// 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
|
||||
g2d.setFont(SCORE_FONT);
|
||||
g2d.setColor(TEXT_COLOR);
|
||||
drawCenteredString(g2d, avgScoreText, xPosition, barWidth, textY);
|
||||
|
||||
g.setColor(Color.BLACK); // Couleur du texte
|
||||
g.drawString(avgScoreText, textX, textY);
|
||||
// Position de "Score moyen"
|
||||
int labelY = textY - 15; // Placer légèrement au-dessus du score
|
||||
if (labelY < PADDING) {
|
||||
labelY = PADDING - 17; // Éviter les débordements
|
||||
}
|
||||
|
||||
// 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
|
||||
g2d.setFont(AVERAGE_FONT);
|
||||
drawCenteredString(g2d, "Score moyen : ", xPosition, barWidth, labelY);
|
||||
|
||||
// Dessiner l'étiquette de groupe (Groupe 1, Groupe 2, ...)
|
||||
g2d.setFont(GROUP_LABEL_FONT);
|
||||
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
|
||||
drawCenteredString(g2d, groupLabel, xPosition, barWidth, height - 10);
|
||||
}
|
||||
} finally {
|
||||
g2d.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calcule la hauteur d'une barre proportionnellement au score maximum.
|
||||
*
|
||||
* @param maxScore Le score maximum parmi tous les groupes
|
||||
* @param height La hauteur totale du composant
|
||||
* @param score Le score du groupe courant
|
||||
* @return La hauteur de la barre
|
||||
*/
|
||||
private int calculateBarHeight(int maxScore, int height, int score) {
|
||||
return (int) ((double) score / maxScore * (height - 2 * PADDING));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dessine une chaîne centrée horizontalement dans une section de la largeur du composant.
|
||||
*
|
||||
* @param g2d Le contexte graphique
|
||||
* @param text Le texte à dessiner
|
||||
* @param xOffset La position x de départ de la section
|
||||
* @param sectionWidth La largeur de la section
|
||||
* @param y La position y où dessiner le texte
|
||||
*/
|
||||
private void drawCenteredString(Graphics2D g2d, String text, int xOffset, int sectionWidth, int y) {
|
||||
FontMetrics metrics = g2d.getFontMetrics();
|
||||
int textWidth = metrics.stringWidth(text);
|
||||
int textX = xOffset + (sectionWidth - textWidth) / 2;
|
||||
g2d.drawString(text, textX, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne la taille préférée du panneau.
|
||||
*
|
||||
* @return La taille préférée
|
||||
* @see Dimension
|
||||
*/
|
||||
@Override
|
||||
public Dimension getPreferredSize() {
|
||||
// Définir une taille préférée par défaut si non spécifiée
|
||||
return new Dimension(400, 200);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -120,7 +120,7 @@ public class GameOver extends JPanel {
|
||||
groupTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
groupPanel.add(groupTitleLabel);
|
||||
|
||||
BarChartPanel barChartPanel = new BarChartPanel(groupAverages, playerGroup - 1, mainPanel);
|
||||
BarChartPanel barChartPanel = new BarChartPanel(groupAverages, playerGroup - 1);
|
||||
barChartPanel.setPreferredSize(new Dimension(700, 400));
|
||||
barChartPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement
|
||||
groupPanel.add(barChartPanel);
|
||||
|
Reference in New Issue
Block a user