Files
BUT3ProjetJeuGroupe/fr/iut_fbleau/Avalam/BoardView.java

193 lines
5.0 KiB
Java
Raw Normal View History

package fr.iut_fbleau.Avalam;
import javax.swing.*;
2025-11-25 16:02:23 -05:00
import java.awt.*;
/**
* La classe <code>BoardView</code>
*
* Représente la vue principale du plateau Avalam.
* Elle gère :
* - laffichage des tours (PieceLayer)
* - laffichage des coups possibles (HighlightLayer)
* - laffichage du fond graphique
2026-01-26 13:41:48 +01:00
* - les clics via InteractionController
*
* Cette classe ne contient aucune logique de règles du jeu.
*
2026-01-26 13:41:48 +01:00
* @version 1.0
* Date :
* Licence :
*/
public class BoardView extends JLayeredPane {
//Attributs
2026-01-26 13:41:48 +01:00
/** Référence au moteur Avalam. */
2025-11-25 16:02:23 -05:00
private AvalamBoard board;
2026-01-26 13:41:48 +01:00
/** Couche daffichage du fond. */
2025-11-27 14:06:05 -05:00
private BackgroundLayer backgroundLayer;
2026-01-26 13:41:48 +01:00
/** Couche daffichage des coups possibles. */
private HighlightLayer highlightLayer;
2026-01-26 13:41:48 +01:00
/** Couche daffichage des pièces. */
private PieceLayer pieceLayer;
2026-01-26 13:41:48 +01:00
/** Contrôleur des interactions. */
private InteractionController controller;
/** Taille dun pion en pixels. */
2025-11-25 16:02:23 -05:00
private final int size = 50;
2026-01-26 13:41:48 +01:00
/** Espacement entre les cases. */
2025-11-25 16:02:23 -05:00
private final int spacing = 70;
/** Décalage horizontal du plateau. */
2025-11-25 16:02:23 -05:00
private final int xBase = 60;
/** Décalage vertical du plateau. */
2025-11-25 16:02:23 -05:00
private final int yBase = 60;
2026-01-26 13:41:48 +01:00
/** Callback vers AvalamWindow pour mise à jour (score, tour, fin). */
2025-11-25 16:02:23 -05:00
private Runnable boardUpdateCallback;
//Constructeur
/**
2026-01-26 13:41:48 +01:00
* Construit la vue du plateau.
*
2026-01-26 13:41:48 +01:00
* @param board moteur du jeu
* @param boardUpdateCallback callback à appeler après un coup
*/
2025-11-25 16:02:23 -05:00
public BoardView(AvalamBoard board, Runnable boardUpdateCallback) {
this.board = board;
this.boardUpdateCallback = boardUpdateCallback;
setLayout(null);
2026-01-26 13:41:48 +01:00
// Contrôleur
2025-11-25 16:02:23 -05:00
this.controller = new InteractionController(board, this);
2026-01-26 13:41:48 +01:00
// Couche fond
2025-11-27 14:06:05 -05:00
backgroundLayer = new BackgroundLayer("fr/iut_fbleau/Res/BackgroundAvalam.png");
backgroundLayer.setBounds(0, 0, 725, 725);
add(backgroundLayer, JLayeredPane.FRAME_CONTENT_LAYER);
2026-01-26 13:41:48 +01:00
// Couche highlight
highlightLayer = new HighlightLayer(xBase, yBase, spacing, size);
add(highlightLayer, JLayeredPane.DEFAULT_LAYER);
2025-11-25 16:02:23 -05:00
2026-01-26 13:41:48 +01:00
// Couche pièces
2025-11-25 16:02:23 -05:00
pieceLayer = new PieceLayer();
add(pieceLayer, JLayeredPane.PALETTE_LAYER);
2025-11-27 14:06:05 -05:00
setPreferredSize(new Dimension(725, 725));
2025-11-25 16:02:23 -05:00
refresh();
}
//Méthodes
2026-01-26 13:41:48 +01:00
/**
* Retourne le contrôleur d'interactions (utile pour simuler les clics du bot).
*
* @return contrôleur
*/
public InteractionController getController() {
return controller;
}
/**
* Active/désactive les interactions utilisateur sur le plateau.
* Utile pendant l'animation du bot pour éviter des clics concurrents.
*
* @param enabled true pour activer, false pour désactiver
*/
public void setInteractionEnabled(boolean enabled) {
// Désactive la couche des pièces (boutons) principalement
pieceLayer.setEnabled(enabled);
for (Component c : pieceLayer.getComponents()) {
c.setEnabled(enabled);
}
}
/**
* Appelé par le contrôleur après un coup.
*/
2025-11-25 16:02:23 -05:00
public void onBoardUpdated() {
if (boardUpdateCallback != null) {
boardUpdateCallback.run();
}
}
/**
2026-01-26 13:41:48 +01:00
* Rafraîchit les couches visuelles.
*/
public void refresh() {
pieceLayer.displayGrid(
2025-11-25 16:02:23 -05:00
boardGrid(),
xBase, yBase, spacing, size,
(r, c) -> controller.onPieceClicked(r, c)
);
2025-11-25 16:02:23 -05:00
highlightLayer.setLegalMoves(controller.getLegalMoves());
2025-11-27 14:06:05 -05:00
backgroundLayer.repaint();
highlightLayer.repaint();
2025-11-25 16:02:23 -05:00
pieceLayer.repaint();
repaint();
}
/**
2026-01-26 13:41:48 +01:00
* Récupère la grille depuis le moteur.
*
* @return grille 9x9 de tours
*/
2025-11-25 16:02:23 -05:00
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;
}
2025-11-27 14:06:05 -05:00
//Affichage
2025-11-27 14:06:05 -05:00
/**
2026-01-26 13:41:48 +01:00
* Composant affichant limage de fond.
*/
2025-11-27 14:06:05 -05:00
private static class BackgroundLayer extends JComponent {
private Image img;
/**
2026-01-26 13:41:48 +01:00
* Construit une couche de fond.
*
2026-01-26 13:41:48 +01:00
* @param resourcePath chemin de l'image de fond
*/
2025-11-27 14:06:05 -05:00
public BackgroundLayer(String resourcePath) {
img = Toolkit.getDefaultToolkit().getImage(
2026-01-26 13:41:48 +01:00
getClass().getClassLoader().getResource(resourcePath)
2025-11-27 14:06:05 -05:00
);
}
/**
2026-01-26 13:41:48 +01:00
* Dessine l'image de fond.
*
* @param g contexte graphique
*/
2025-11-27 14:06:05 -05:00
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
}
}