Closed Starlight-30036225 closed 7 months ago
For displaying the board and then the sprites on the frame: ` public void paint(Graphics g) { g.setFont(new Font("Serif", BOLD, 14));
boolean white = true; //FlipFlops
for (int Y = 0; Y < 8; Y++) { //8 rows and 8 cols
// add text to label
g.setColor(Color.BLACK);
g.drawString(String.valueOf(Y + 1), MARGIN/2, (int) (Y * 64 + (MARGIN * 2.3)));
g.drawString(String.valueOf((char) ((Y ) + 97)), (int) (Y * 64 + (MARGIN * 2)),MARGIN/2);
for (int X = 0; X < 8; X++) {
if (white) {
g.setColor(Color.YELLOW);
} else {
g.setColor(Color.PINK);
}
white = !white; //flips the flop
g.fillRect(X * 64 + MARGIN, Y * 64 + MARGIN, 64, 64);
}
white = !white;
}
DrawPieces(g);
}`
This function draws an 8 by 8 board, and label them with ranks and files. Then calls the draw pieces function to handle the pieces
` private void DrawPieces(Graphics g) { for (SimplePiece[] Row : Board) { for (SimplePiece p: Row) { if (p == null) {continue;} int imageVal = p.getImageval(); if (p == SelectedPiece) { g.drawImage(imgList[imageVal], MouseX, MouseY, this); } else{ g.drawImage(imgList[imageVal], p.getX() SQUARE_SIZE + MARGIN, p.getY() SQUARE_SIZE+MARGIN, this); }
}
}
}
};
`
This loops through all spaces on the board to check for a valid piece, then uses its piece class to select the appropriate image for that piece.
public int getImageval() { return type.BaseImageVal + (white? 0 : 6); }
This function is from inside the simple Piece class, and uses the type enum to find the base image value (which is always the same piece in white) then if the colour is white it is fine, if its is black it adds 6 (moving its index to the corresponding black piece in the sprite array)
I have had to seperate the swift across two classes, One that handles the display of the board, and one to handle inputs.
The display class reads the current state of the board (or would if i had this implemented) and displays it.
` private static JFrame getFrame() {
This is the basic setup for the Jframe, it uses 3 constants for the size of the window
` private static void ExtractImages() { BufferedImage BaseImage;
This snippet extracts sprites from the pieces from a sprite sheet, and saves them to a global array