Files
BUT3ProjetJeuGroupe/fr/iut_fbleau/Avalam/ui/BoardView.java
2025-11-27 14:06:05 -05:00

137 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package fr.iut_fbleau.Avalam.ui;
import fr.iut_fbleau.Avalam.AvalamBoard;
import fr.iut_fbleau.Avalam.Tower;
import javax.swing.*;
import java.awt.*;
/**
* BoardView est la vue principale du plateau Avalam.
*
* Elle gère :
* - laffichage des tours (PieceLayer)
* - laffichage des coups possibles (HighlightLayer)
* - un fond graphique personnalisé
* - les clics via InteractionController
*/
public class BoardView extends JLayeredPane {
/** Référence au moteur Avalam */
private AvalamBoard board;
/** Couche daffichage du fond */
private BackgroundLayer backgroundLayer;
/** Couche daffichage des rond verts */
private HighlightLayer highlightLayer;
/** Couche daffichage des pièces */
private PieceLayer pieceLayer;
/** Contrôleur des interactions */
private InteractionController controller;
/** Décalages et dimensions pour l'affichage */
private final int size = 50;
private final int spacing = 70;
private final int xBase = 60;
private final int yBase = 60;
/** Callback vers AvalamWindow pour mises à jour (score, tour, fin) */
private Runnable boardUpdateCallback;
/**
* Constructeur.
*/
public BoardView(AvalamBoard board, Runnable boardUpdateCallback) {
this.board = board;
this.boardUpdateCallback = boardUpdateCallback;
setLayout(null);
// --- Contrôleur ---
this.controller = new InteractionController(board, this);
// --- Couche fond ---
backgroundLayer = new BackgroundLayer("fr/iut_fbleau/Res/BackgroundAvalam.png");
backgroundLayer.setBounds(0, 0, 725, 725);
add(backgroundLayer, JLayeredPane.FRAME_CONTENT_LAYER);
// --- Couche highlight ---
highlightLayer = new HighlightLayer(xBase, yBase, spacing, size);
add(highlightLayer, JLayeredPane.DEFAULT_LAYER);
// --- Couche des pièces ---
pieceLayer = new PieceLayer();
add(pieceLayer, JLayeredPane.PALETTE_LAYER);
setPreferredSize(new Dimension(725, 725));
refresh();
}
/**
* Appelé par le contrôleur après un coup.
*/
public void onBoardUpdated() {
if (boardUpdateCallback != null) {
boardUpdateCallback.run();
}
}
/**
* Rafraîchit les couches visuelles.
*/
public void refresh() {
pieceLayer.displayGrid(
boardGrid(),
xBase, yBase, spacing, size,
(r, c) -> controller.onPieceClicked(r, c)
);
highlightLayer.setLegalMoves(controller.getLegalMoves());
backgroundLayer.repaint();
highlightLayer.repaint();
pieceLayer.repaint();
repaint();
}
/**
* Récupère la grille depuis le moteur.
*/
private Tower[][] boardGrid() {
Tower[][] grid = new Tower[AvalamBoard.SIZE][AvalamBoard.SIZE];
for (int r = 0; r < AvalamBoard.SIZE; r++) {
for (int c = 0; c < AvalamBoard.SIZE; c++) {
grid[r][c] = board.getTowerAt(r, c);
}
}
return grid;
}
/**
* Composant affichant limage de fond.
*/
private static class BackgroundLayer extends JComponent {
private Image img;
public BackgroundLayer(String resourcePath) {
img = Toolkit.getDefaultToolkit().getImage(
getClass().getClassLoader().getResource(resourcePath)
);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
}
}