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.controller.GameModeController;
|
||||||
import fr.monkhanny.dorfromantik.gui.GameModeSelectionPanel;
|
import fr.monkhanny.dorfromantik.gui.GameModeSelectionPanel;
|
||||||
|
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,14 +77,21 @@ public class Main {
|
|||||||
isMusicPlayed = true; // Marquer la musique comme jouée
|
isMusicPlayed = true; // Marquer la musique comme jouée
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 4. Créer les fenêtres à nouveau comme au début
|
// 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 = new JFrame("Choix des séries - Dorfromantik");
|
||||||
gameModeFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
gameModeFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||||
|
gameModeFrame.setIconImage(icon.getImage());
|
||||||
gameFrame = new JFrame("Jeu - Dorfromantik");
|
gameFrame = new JFrame("Jeu - Dorfromantik");
|
||||||
|
gameFrame.setIconImage(icon.getImage());
|
||||||
gameFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
gameFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||||
settingsFrame = new JFrame("Paramètres - Dorfromantik");
|
settingsFrame = new JFrame("Paramètres - Dorfromantik");
|
||||||
|
settingsFrame.setIconImage(icon.getImage());
|
||||||
settingsFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
settingsFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||||
howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik");
|
howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik");
|
||||||
|
howToPlayFrame.setIconImage(icon.getImage());
|
||||||
howToPlayFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
howToPlayFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
|
||||||
|
|
||||||
// Re-créer et réinitialiser les panels et les contrôleurs
|
// 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.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class BarChartPanel extends JPanel {
|
public class BarChartPanel extends JPanel {
|
||||||
private List<Integer> groupAverages;
|
private final List<Integer> groupAverages;
|
||||||
private int highlightedGroup;
|
private final int highlightedGroup;
|
||||||
|
private static final Color PLAYER_GROUP_COLOR = new Color(204, 0, 0);
|
||||||
public BarChartPanel(List<Integer> groupAverages, int highlightedGroup, JPanel mainPanel) {
|
private static final Color OTHER_GROUP_COLOR = new Color(0, 0, 204);
|
||||||
this.groupAverages = groupAverages;
|
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) {
|
||||||
|
this.groupAverages = Objects.requireNonNull(groupAverages, "Group averages cannot be null");
|
||||||
this.highlightedGroup = highlightedGroup;
|
this.highlightedGroup = highlightedGroup;
|
||||||
|
|
||||||
// Rendre le fond transparent et ajouter une bordure noire
|
// Configuration du panneau
|
||||||
setBackground(new Color(0, 0, 0, 0)); // Fond transparent
|
setOpaque(false);
|
||||||
setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement
|
setBackground(new Color(0, 0, 0, 0));
|
||||||
|
setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
int width = getWidth();
|
|
||||||
int height = getHeight();
|
if (groupAverages.isEmpty()) {
|
||||||
int barWidth = width / (groupAverages.size() + 1); // Espacement entre les groupes
|
return;
|
||||||
int maxScore = groupAverages.stream().max(Integer::compare).orElse(0);
|
}
|
||||||
|
|
||||||
// Dessiner les barres et leurs étiquettes (moyennes des scores)
|
Graphics2D g2d = (Graphics2D) g.create();
|
||||||
for (int i = 0; i < groupAverages.size(); i++) {
|
try {
|
||||||
int barHeight = (int) ((double) groupAverages.get(i) / maxScore * (height - 50)); // Ajuster la hauteur des barres
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
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)
|
int width = getWidth();
|
||||||
if (i == highlightedGroup) {
|
int height = getHeight();
|
||||||
g.setColor(new Color(204, 0, 0)); // Couleur rouge pour le groupe du joueur
|
int barWidth = width / (groupAverages.size() + 1);
|
||||||
} else {
|
int maxScore = groupAverages.stream()
|
||||||
g.setColor(new Color(0, 0, 204)); // Bleu pour les autres groupes
|
.mapToInt(Integer::intValue)
|
||||||
|
.max()
|
||||||
|
.orElse(0);
|
||||||
|
|
||||||
|
for (int i = 0; i < groupAverages.size(); i++) {
|
||||||
|
int barHeight = calculateBarHeight(maxScore, height, groupAverages.get(i));
|
||||||
|
int xPosition = (i + 1) * barWidth;
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
String avgScoreText = String.valueOf(groupAverages.get(i));
|
||||||
|
g2d.setFont(SCORE_FONT);
|
||||||
|
g2d.setColor(TEXT_COLOR);
|
||||||
|
drawCenteredString(g2d, avgScoreText, xPosition, barWidth, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
g2d.setFont(AVERAGE_FONT);
|
||||||
|
drawCenteredString(g2d, "Score moyen : ", xPosition, barWidth, labelY);
|
||||||
|
|
||||||
|
g2d.setFont(GROUP_LABEL_FONT);
|
||||||
|
String groupLabel = "Groupe " + (i + 1);
|
||||||
|
drawCenteredString(g2d, groupLabel, xPosition, barWidth, height - 10);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
g2d.dispose();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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));
|
* Calcule la hauteur d'une barre proportionnellement au score maximum.
|
||||||
FontMetrics metrics = g.getFontMetrics();
|
*
|
||||||
int textWidth = metrics.stringWidth(avgScoreText);
|
* @param maxScore Le score maximum parmi tous les groupes
|
||||||
int textX = xPosition + (barWidth - textWidth) / 2; // Centrer le texte
|
* @param height La hauteur totale du composant
|
||||||
int textY = height - barHeight - 35; // Placer le texte juste au-dessus de la barre
|
* @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));
|
||||||
|
}
|
||||||
|
|
||||||
g.setColor(Color.BLACK); // Couleur du texte
|
/**
|
||||||
g.drawString(avgScoreText, textX, textY);
|
* Dessine une chaîne centrée horizontalement dans une section de la largeur du composant.
|
||||||
|
*
|
||||||
// Ajouter un label pour préciser que c'est la "moyenne"
|
* @param g2d Le contexte graphique
|
||||||
g.setFont(new Font("Arial", Font.ITALIC, 12)); // Police italique plus petite pour le label "Moyenne"
|
* @param text Le texte à dessiner
|
||||||
String label = "Score moyen : ";
|
* @param xOffset La position x de départ de la section
|
||||||
int labelWidth = metrics.stringWidth(label);
|
* @param sectionWidth La largeur de la section
|
||||||
g.drawString(label, xPosition + (barWidth - labelWidth) / 2, textY - 15); // Placer "Moyenne" au-dessus du score
|
* @param y La position y où dessiner le texte
|
||||||
|
*/
|
||||||
// Dessiner l'étiquette de groupe (Groupe 1, Groupe 2, ...)
|
private void drawCenteredString(Graphics2D g2d, String text, int xOffset, int sectionWidth, int y) {
|
||||||
String groupLabel = "Groupe " + (i + 1);
|
FontMetrics metrics = g2d.getFontMetrics();
|
||||||
g.setColor(Color.BLACK);
|
int textWidth = metrics.stringWidth(text);
|
||||||
g.setFont(new Font("Arial", Font.PLAIN, 14)); // Police plus petite pour les étiquettes
|
int textX = xOffset + (sectionWidth - textWidth) / 2;
|
||||||
int groupLabelWidth = g.getFontMetrics().stringWidth(groupLabel);
|
g2d.drawString(text, textX, y);
|
||||||
g.drawString(groupLabel, xPosition + (barWidth - groupLabelWidth) / 2, height - 10); // Positionner l'étiquette sous la barre
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
groupTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
groupPanel.add(groupTitleLabel);
|
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.setPreferredSize(new Dimension(700, 400));
|
||||||
barChartPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement
|
barChartPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement
|
||||||
groupPanel.add(barChartPanel);
|
groupPanel.add(barChartPanel);
|
||||||
|
Reference in New Issue
Block a user