Actualiser src/windowInterface/JPanelChessBoard.java
This commit is contained in:
parent
e80133174f
commit
e3b81bf579
|
|
@ -19,18 +19,23 @@ import backend.PieceType;
|
||||||
public class JPanelChessBoard extends JPanel {
|
public class JPanelChessBoard extends JPanel {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private Game myGame;
|
private Game myGame; // Reference to the game logic
|
||||||
private MyInterface interfaceGlobal;
|
private MyInterface interfaceGlobal; // Reference to the main interface
|
||||||
private BufferedImage spriteSheet;
|
private BufferedImage spriteSheet; // Image containing all chess pieces
|
||||||
private int PIECE_WIDTH = 16; //in spritesheet
|
private int PIECE_WIDTH = 16; // Width of each piece in the spritesheet (pixels)
|
||||||
private int PIECE_HEIGHT = 16; //in spritesheet
|
private int PIECE_HEIGHT = 16; // Height of each piece in the spritesheet (pixels)
|
||||||
private int MARGIN = 6;
|
private int MARGIN = 6; // Margin around pieces when drawn on board
|
||||||
|
|
||||||
private boolean pieceSelectorMode;
|
// Mode flags to track panel's current state
|
||||||
private boolean selectedPieceIsWhite;
|
private boolean pieceSelectorMode; // Whether piece selection UI is active
|
||||||
private PieceType selectedPieceType;
|
private boolean selectedPieceIsWhite; // Color of currently selected piece
|
||||||
private boolean pieceAdderMode;
|
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) {
|
public JPanelChessBoard(MyInterface itf) {
|
||||||
super();
|
super();
|
||||||
myGame = null;
|
myGame = null;
|
||||||
|
|
@ -45,42 +50,58 @@ public class JPanelChessBoard extends JPanel {
|
||||||
}
|
}
|
||||||
pieceSelectorMode = false;
|
pieceSelectorMode = false;
|
||||||
pieceAdderMode = false;
|
pieceAdderMode = false;
|
||||||
|
|
||||||
|
// Add mouse listener to handle clicks on the board
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent me) {
|
public void mousePressed(MouseEvent me) {
|
||||||
// System.out.println(me);
|
// Handle mouse clicks based on current mode
|
||||||
if(pieceSelectorMode) {
|
if(pieceSelectorMode) {
|
||||||
|
// In piece selector mode, choose piece based on click location
|
||||||
int x = Math.round(me.getX()/cellWidth());
|
int x = Math.round(me.getX()/cellWidth());
|
||||||
selectedPieceType = PieceType.values()[5-x];
|
selectedPieceType = PieceType.values()[5-x];
|
||||||
selectedPieceIsWhite = (me.getY() > cellHeight());
|
selectedPieceIsWhite = (me.getY() > cellHeight());
|
||||||
pieceSelectorMode = false;
|
pieceSelectorMode = false;
|
||||||
} else {
|
} else {
|
||||||
|
// Create game if it doesn't exist
|
||||||
if(myGame == null) {
|
if(myGame == null) {
|
||||||
interfaceGlobal.instantiateSimu();
|
interfaceGlobal.instantiateSimu();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert screen coordinates to game coordinates
|
||||||
int x = (me.getX()*myGame.getWidth())/getWidth();
|
int x = (me.getX()*myGame.getWidth())/getWidth();
|
||||||
int y = (me.getY()*myGame.getHeight())/getHeight();
|
int y = (me.getY()*myGame.getHeight())/getHeight();
|
||||||
|
|
||||||
if(pieceAdderMode) {
|
if(pieceAdderMode) {
|
||||||
//TODO
|
// Add new piece to the board at clicked location
|
||||||
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
myGame.setPiece(selectedPieceIsWhite, selectedPieceType, x, y);
|
||||||
pieceAdderMode = false;
|
pieceAdderMode = false;
|
||||||
} else {
|
} 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) {
|
public void setGame(Game simu) {
|
||||||
myGame = simu;
|
myGame = simu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the chess board and pieces
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
this.setBackground(Color.black);
|
this.setBackground(Color.black);
|
||||||
|
|
||||||
|
// If in piece selector mode, just draw the piece selection UI
|
||||||
if(pieceSelectorMode) {
|
if(pieceSelectorMode) {
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
spriteSheet,
|
spriteSheet,
|
||||||
|
|
@ -92,22 +113,28 @@ public class JPanelChessBoard extends JPanel {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myGame != null) {
|
if (myGame != null) {
|
||||||
// Draw Interface from state of simulator
|
// Calculate cell dimensions
|
||||||
float cellWidth = cellWidth();
|
float cellWidth = cellWidth();
|
||||||
float cellHeight = cellHeight();
|
float cellHeight = cellHeight();
|
||||||
|
|
||||||
|
// Draw the board squares
|
||||||
g.setColor(Color.white);
|
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++) {
|
for (int y=0; y<myGame.getHeight(); y++) {
|
||||||
boolean isSelect = myGame.isSelected(x,y);
|
boolean isSelect = myGame.isSelected(x,y);
|
||||||
boolean isHighlight = myGame.isHighlighted(x,y);
|
boolean isHighlight = myGame.isHighlighted(x,y);
|
||||||
|
|
||||||
|
// Set colors for selected or highlighted squares
|
||||||
if(isSelect) {
|
if(isSelect) {
|
||||||
g.setColor(Color.blue);
|
g.setColor(Color.blue);
|
||||||
}
|
}
|
||||||
if(isHighlight) {
|
if(isHighlight) {
|
||||||
g.setColor(Color.yellow);
|
g.setColor(Color.yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw white squares or highlighted/selected squares
|
||||||
if((x+y)%2==1 || isSelect || isHighlight) {
|
if((x+y)%2==1 || isSelect || isHighlight) {
|
||||||
g.fillRect(
|
g.fillRect(
|
||||||
Math.round(x*cellWidth),
|
Math.round(x*cellWidth),
|
||||||
|
|
@ -116,15 +143,17 @@ public class JPanelChessBoard extends JPanel {
|
||||||
Math.round(cellHeight)
|
Math.round(cellHeight)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset color after drawing special squares
|
||||||
if(isHighlight || isSelect) {
|
if(isHighlight || isSelect) {
|
||||||
g.setColor(Color.white);
|
g.setColor(Color.white);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw grid lines
|
||||||
g.setColor(Color.gray);
|
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);
|
int graphX = Math.round(x*cellWidth);
|
||||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||||
}
|
}
|
||||||
|
|
@ -133,12 +162,18 @@ public class JPanelChessBoard extends JPanel {
|
||||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw all chess pieces
|
||||||
for (Piece piece : myGame.getPieces()) {
|
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) {
|
private void drawPiece(Graphics g, Piece piece) {
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
|
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) {
|
private Image getChessPieceImageFromType(PieceType type, boolean isWhite) {
|
||||||
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
||||||
int y = PIECE_HEIGHT * (isWhite?1:0);
|
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) {
|
private int spriteSheetPositionOfPieceType(PieceType type) {
|
||||||
return 5-type.ordinal();
|
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() {
|
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() {
|
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) {
|
private int xCoordFromGame(int x) {
|
||||||
return Math.round(x*cellWidth());
|
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) {
|
private int yCoordFromGame(int y) {
|
||||||
return Math.round(y*cellHeight());
|
return Math.round(y*cellHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggles piece selector mode on/off
|
||||||
|
*/
|
||||||
public void togglePieceSelector() {
|
public void togglePieceSelector() {
|
||||||
pieceSelectorMode = ! pieceSelectorMode;
|
pieceSelectorMode = !pieceSelectorMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggles piece adder mode on/off
|
||||||
|
*/
|
||||||
public void toggleAdderMode() {
|
public void toggleAdderMode() {
|
||||||
pieceAdderMode = ! pieceAdderMode;
|
pieceAdderMode = !pieceAdderMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Whether piece selector mode is active
|
||||||
|
*/
|
||||||
public boolean isPieceSelectorMode() {
|
public boolean isPieceSelectorMode() {
|
||||||
return pieceSelectorMode;
|
return pieceSelectorMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Whether piece adder mode is active
|
||||||
|
*/
|
||||||
public boolean isPieceAdderMode() {
|
public boolean isPieceAdderMode() {
|
||||||
return pieceAdderMode;
|
return pieceAdderMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue