Actualiser src/windowInterface/JPanelChessBoard.java

This commit is contained in:
Guillaume VALLENET 2025-04-25 16:30:07 +02:00
parent e80133174f
commit e3b81bf579
1 changed files with 104 additions and 28 deletions

View File

@ -19,18 +19,23 @@ import backend.PieceType;
public class JPanelChessBoard extends JPanel {
private static final long serialVersionUID = 1L;
private Game myGame;
private MyInterface interfaceGlobal;
private BufferedImage spriteSheet;
private int PIECE_WIDTH = 16; //in spritesheet
private int PIECE_HEIGHT = 16; //in spritesheet
private int MARGIN = 6;
private Game myGame; // Reference to the game logic
private MyInterface interfaceGlobal; // Reference to the main interface
private BufferedImage spriteSheet; // Image containing all chess pieces
private int PIECE_WIDTH = 16; // Width of each piece in the spritesheet (pixels)
private int PIECE_HEIGHT = 16; // Height of each piece in the spritesheet (pixels)
private int MARGIN = 6; // Margin around pieces when drawn on board
private boolean pieceSelectorMode;
private boolean selectedPieceIsWhite;
private PieceType selectedPieceType;
private boolean pieceAdderMode;
// Mode flags to track panel's current state
private boolean pieceSelectorMode; // Whether piece selection UI is active
private boolean selectedPieceIsWhite; // Color of currently selected piece
private PieceType selectedPieceType; // Type of currently selected piece
private boolean pieceAdderMode; // Whether piece adding mode is active
/**
* Constructor for the chess board panel
* @param itf Reference to the main interface
*/
public JPanelChessBoard(MyInterface itf) {
super();
myGame = null;
@ -45,42 +50,58 @@ public class JPanelChessBoard extends JPanel {
}
pieceSelectorMode = false;
pieceAdderMode = false;
// Add mouse listener to handle clicks on the board
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
// System.out.println(me);
// Handle mouse clicks based on current mode
if(pieceSelectorMode) {
// In piece selector mode, choose piece based on click location
int x = Math.round(me.getX()/cellWidth());
selectedPieceType = PieceType.values()[5-x];
selectedPieceIsWhite = (me.getY() > cellHeight());
pieceSelectorMode = false;
} else {
// Create game if it doesn't exist
if(myGame == null) {
interfaceGlobal.instantiateSimu();
}
// Convert screen coordinates to game coordinates
int x = (me.getX()*myGame.getWidth())/getWidth();
int y = (me.getY()*myGame.getHeight())/getHeight();
if(pieceAdderMode) {
//TODO
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
// Add new piece to the board at clicked location
myGame.setPiece(selectedPieceIsWhite, selectedPieceType, x, y);
pieceAdderMode = false;
} else {
myGame.clickCoords(x,y);
// Normal play mode - handle click in game logic
myGame.clickCoords(x, y);
}
}
repaint();
repaint(); // Redraw the board after handling click
}
});
}
/**
* Sets the game instance for this board
* @param simu Game instance to use
*/
public void setGame(Game simu) {
myGame = simu;
}
/**
* Renders the chess board and pieces
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.black);
// If in piece selector mode, just draw the piece selection UI
if(pieceSelectorMode) {
g.drawImage(
spriteSheet,
@ -92,22 +113,28 @@ public class JPanelChessBoard extends JPanel {
);
return;
}
if (myGame != null) {
// Draw Interface from state of simulator
// Calculate cell dimensions
float cellWidth = cellWidth();
float cellHeight = cellHeight();
// Draw the board squares
g.setColor(Color.white);
for(int x=0; x<myGame.getWidth();x++) {
for(int x=0; x<myGame.getWidth(); x++) {
for (int y=0; y<myGame.getHeight(); y++) {
boolean isSelect = myGame.isSelected(x,y);
boolean isHighlight = myGame.isHighlighted(x,y);
// Set colors for selected or highlighted squares
if(isSelect) {
g.setColor(Color.blue);
}
if(isHighlight) {
g.setColor(Color.yellow);
}
// Draw white squares or highlighted/selected squares
if((x+y)%2==1 || isSelect || isHighlight) {
g.fillRect(
Math.round(x*cellWidth),
@ -116,15 +143,17 @@ public class JPanelChessBoard extends JPanel {
Math.round(cellHeight)
);
}
// Reset color after drawing special squares
if(isHighlight || isSelect) {
g.setColor(Color.white);
}
}
}
// Draw grid lines
g.setColor(Color.gray);
for(int x=0; x<myGame.getWidth();x++) {
for(int x=0; x<myGame.getWidth(); x++) {
int graphX = Math.round(x*cellWidth);
g.drawLine(graphX, 0, graphX, this.getHeight());
}
@ -133,12 +162,18 @@ public class JPanelChessBoard extends JPanel {
g.drawLine(0, graphY, this.getWidth(), graphY);
}
// Draw all chess pieces
for (Piece piece : myGame.getPieces()) {
drawPiece(g,piece);
drawPiece(g, piece);
}
}
}
/**
* Draws a single chess piece at its position
* @param g Graphics context
* @param piece The piece to draw
*/
private void drawPiece(Graphics g, Piece piece) {
g.drawImage(
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
@ -148,7 +183,12 @@ public class JPanelChessBoard extends JPanel {
);
}
/**
* Gets the appropriate image for a chess piece from the sprite sheet
* @param type Type of piece (Pawn, Rook, etc.)
* @param isWhite Whether the piece is white
* @return Image of the requested chess piece
*/
private Image getChessPieceImageFromType(PieceType type, boolean isWhite) {
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
int y = PIECE_HEIGHT * (isWhite?1:0);
@ -159,38 +199,74 @@ public class JPanelChessBoard extends JPanel {
);
}
/**
* Maps piece type to its position in the sprite sheet
* @param type The piece type
* @return Column index in the sprite sheet
*/
private int spriteSheetPositionOfPieceType(PieceType type) {
return 5-type.ordinal();
}
/**
* Calculates the width of a single cell on the board
* @return Width of a board cell in pixels
*/
private float cellWidth() {
return (float) this.getWidth()/ (float)myGame.getWidth();
return (float) this.getWidth() / (float)myGame.getWidth();
}
/**
* Calculates the height of a single cell on the board
* @return Height of a board cell in pixels
*/
private float cellHeight() {
return (float)this.getHeight()/ (float)myGame.getHeight();
return (float)this.getHeight() / (float)myGame.getHeight();
}
/**
* Converts game x-coordinate to screen pixel coordinate
* @param x Game x-coordinate
* @return Screen x-coordinate in pixels
*/
private int xCoordFromGame(int x) {
return Math.round(x*cellWidth());
}
/**
* Converts game y-coordinate to screen pixel coordinate
* @param y Game y-coordinate
* @return Screen y-coordinate in pixels
*/
private int yCoordFromGame(int y) {
return Math.round(y*cellHeight());
}
/**
* Toggles piece selector mode on/off
*/
public void togglePieceSelector() {
pieceSelectorMode = ! pieceSelectorMode;
pieceSelectorMode = !pieceSelectorMode;
}
/**
* Toggles piece adder mode on/off
*/
public void toggleAdderMode() {
pieceAdderMode = ! pieceAdderMode;
pieceAdderMode = !pieceAdderMode;
}
/**
* @return Whether piece selector mode is active
*/
public boolean isPieceSelectorMode() {
return pieceSelectorMode;
}
/**
* @return Whether piece adder mode is active
*/
public boolean isPieceAdderMode() {
return pieceAdderMode;
}
}