diff --git a/src/windowInterface/JPanelChessBoard.java b/src/windowInterface/JPanelChessBoard.java index ad260b1..04f48ca 100644 --- a/src/windowInterface/JPanelChessBoard.java +++ b/src/windowInterface/JPanelChessBoard.java @@ -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