|
|
|
|
@ -25,6 +25,13 @@ public class JPanelChessBoard extends JPanel {
|
|
|
|
|
private int PIECE_WIDTH = 16; //in spritesheet
|
|
|
|
|
private int PIECE_HEIGHT = 16; //in spritesheet
|
|
|
|
|
private int MARGIN = 6;
|
|
|
|
|
private int BORDER_WIDTH = 20; // Width of the border around the board
|
|
|
|
|
|
|
|
|
|
// Wood-like colors
|
|
|
|
|
private Color lightWoodColor = new Color(240, 217, 181); // Light maple
|
|
|
|
|
private Color darkWoodColor = new Color(181, 136, 99); // Dark mahogany
|
|
|
|
|
private Color borderColor = new Color(101, 67, 33); // Dark walnut
|
|
|
|
|
private Color borderInnerColor = new Color(140, 94, 47); // Medium walnut
|
|
|
|
|
|
|
|
|
|
private boolean pieceSelectorMode;
|
|
|
|
|
private boolean selectedPieceIsWhite;
|
|
|
|
|
@ -57,22 +64,31 @@ public class JPanelChessBoard extends JPanel {
|
|
|
|
|
if(myGame == null) {
|
|
|
|
|
interfaceGlobal.instantiateSimu();
|
|
|
|
|
}
|
|
|
|
|
int x = (me.getX()*myGame.getWidth())/getWidth();
|
|
|
|
|
int y = (me.getY()*myGame.getHeight())/getHeight();
|
|
|
|
|
// Adjust for border
|
|
|
|
|
int adjustedX = me.getX() - BORDER_WIDTH;
|
|
|
|
|
int adjustedY = me.getY() - BORDER_WIDTH;
|
|
|
|
|
|
|
|
|
|
// Only process clicks within the actual board area
|
|
|
|
|
if (adjustedX >= 0 && adjustedY >= 0 &&
|
|
|
|
|
adjustedX < (getWidth() - 2*BORDER_WIDTH) &&
|
|
|
|
|
adjustedY < (getHeight() - 2*BORDER_WIDTH)) {
|
|
|
|
|
|
|
|
|
|
int x = (adjustedX*myGame.getWidth())/(getWidth() - 2*BORDER_WIDTH);
|
|
|
|
|
int y = (adjustedY*myGame.getHeight())/(getHeight() - 2*BORDER_WIDTH);
|
|
|
|
|
|
|
|
|
|
if(pieceAdderMode) {
|
|
|
|
|
//TODO
|
|
|
|
|
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
|
|
|
|
pieceAdderMode = false;
|
|
|
|
|
} else {
|
|
|
|
|
myGame.clickCoords(x,y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setGame(Game simu) {
|
|
|
|
|
myGame = simu;
|
|
|
|
|
}
|
|
|
|
|
@ -80,7 +96,8 @@ public class JPanelChessBoard extends JPanel {
|
|
|
|
|
@Override
|
|
|
|
|
protected void paintComponent(Graphics g) {
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
|
this.setBackground(Color.black);
|
|
|
|
|
this.setBackground(borderColor); // Dark wood background
|
|
|
|
|
|
|
|
|
|
if(pieceSelectorMode) {
|
|
|
|
|
g.drawImage(
|
|
|
|
|
spriteSheet,
|
|
|
|
|
@ -92,63 +109,120 @@ public class JPanelChessBoard extends JPanel {
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (myGame != null) {
|
|
|
|
|
// Draw Interface from state of simulator
|
|
|
|
|
// Draw the border
|
|
|
|
|
drawWoodenBorder(g);
|
|
|
|
|
|
|
|
|
|
// Draw the chess board with wood colors
|
|
|
|
|
float cellWidth = cellWidth();
|
|
|
|
|
float cellHeight = cellHeight();
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// Calculate position with border
|
|
|
|
|
int posX = BORDER_WIDTH + Math.round(x*cellWidth);
|
|
|
|
|
int posY = BORDER_WIDTH + Math.round(y*cellHeight);
|
|
|
|
|
|
|
|
|
|
// Draw wood-colored square
|
|
|
|
|
if ((x+y)%2 == 0) {
|
|
|
|
|
g.setColor(lightWoodColor);
|
|
|
|
|
} else {
|
|
|
|
|
g.setColor(darkWoodColor);
|
|
|
|
|
}
|
|
|
|
|
g.fillRect(posX, posY, Math.round(cellWidth), Math.round(cellHeight));
|
|
|
|
|
|
|
|
|
|
// Draw selection/highlight overlay
|
|
|
|
|
if(isSelect) {
|
|
|
|
|
g.setColor(Color.blue);
|
|
|
|
|
g.setColor(new Color(0, 0, 255, 128)); // Semi-transparent blue
|
|
|
|
|
g.fillRect(posX, posY, Math.round(cellWidth), Math.round(cellHeight));
|
|
|
|
|
} else if(isHighlight) {
|
|
|
|
|
g.setColor(new Color(255, 255, 0, 128)); // Semi-transparent yellow
|
|
|
|
|
g.fillRect(posX, posY, Math.round(cellWidth), Math.round(cellHeight));
|
|
|
|
|
}
|
|
|
|
|
if(isHighlight) {
|
|
|
|
|
g.setColor(Color.yellow);
|
|
|
|
|
}
|
|
|
|
|
if((x+y)%2==1 || isSelect || isHighlight) {
|
|
|
|
|
g.fillRect(
|
|
|
|
|
Math.round(x*cellWidth),
|
|
|
|
|
Math.round(y*cellHeight),
|
|
|
|
|
Math.round(cellWidth),
|
|
|
|
|
Math.round(cellHeight)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if(isHighlight || isSelect) {
|
|
|
|
|
g.setColor(Color.white);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.setColor(Color.gray);
|
|
|
|
|
for(int x=0; x<myGame.getWidth();x++) {
|
|
|
|
|
int graphX = Math.round(x*cellWidth);
|
|
|
|
|
g.drawLine(graphX, 0, graphX, this.getHeight());
|
|
|
|
|
// Draw grid lines
|
|
|
|
|
g.setColor(new Color(0, 0, 0, 80)); // Semi-transparent black
|
|
|
|
|
for(int x=0; x<=myGame.getWidth(); x++) {
|
|
|
|
|
int graphX = BORDER_WIDTH + Math.round(x*cellWidth);
|
|
|
|
|
g.drawLine(graphX, BORDER_WIDTH, graphX, getHeight() - BORDER_WIDTH);
|
|
|
|
|
}
|
|
|
|
|
for (int y=0; y<myGame.getHeight(); y++) {
|
|
|
|
|
int graphY = Math.round(y*cellHeight);
|
|
|
|
|
g.drawLine(0, graphY, this.getWidth(), graphY);
|
|
|
|
|
for (int y=0; y<=myGame.getHeight(); y++) {
|
|
|
|
|
int graphY = BORDER_WIDTH + Math.round(y*cellHeight);
|
|
|
|
|
g.drawLine(BORDER_WIDTH, graphY, getWidth() - BORDER_WIDTH, graphY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw pieces
|
|
|
|
|
for (Piece piece : myGame.getPieces()) {
|
|
|
|
|
drawPiece(g,piece);
|
|
|
|
|
drawPiece(g, piece);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void drawWoodenBorder(Graphics g) {
|
|
|
|
|
// Draw a rich wooden border around the board
|
|
|
|
|
|
|
|
|
|
// Outer border - dark brown
|
|
|
|
|
g.setColor(borderColor);
|
|
|
|
|
g.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
|
|
|
|
|
|
// Inner border - medium brown
|
|
|
|
|
g.setColor(borderInnerColor);
|
|
|
|
|
g.fillRect(5, 5, getWidth() - 10, getHeight() - 10);
|
|
|
|
|
|
|
|
|
|
// Add wood grain effect with thin lines
|
|
|
|
|
g.setColor(new Color(0, 0, 0, 30)); // Very transparent black
|
|
|
|
|
for (int i = 0; i < getWidth(); i += 7) {
|
|
|
|
|
g.drawLine(i, 0, i, getHeight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Coordinates
|
|
|
|
|
g.setColor(Color.WHITE);
|
|
|
|
|
float cellWidth = cellWidth();
|
|
|
|
|
float cellHeight = cellHeight();
|
|
|
|
|
|
|
|
|
|
// Draw file letters (A-H)
|
|
|
|
|
for (int x = 0; x < myGame.getWidth(); x++) {
|
|
|
|
|
String letter = Character.toString((char)('A' + x));
|
|
|
|
|
g.drawString(letter,
|
|
|
|
|
BORDER_WIDTH + Math.round(x*cellWidth) + Math.round(cellWidth/2) - 4,
|
|
|
|
|
BORDER_WIDTH - 5);
|
|
|
|
|
g.drawString(letter,
|
|
|
|
|
BORDER_WIDTH + Math.round(x*cellWidth) + Math.round(cellWidth/2) - 4,
|
|
|
|
|
getHeight() - BORDER_WIDTH + 15);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw rank numbers (1-8)
|
|
|
|
|
for (int y = 0; y < myGame.getHeight(); y++) {
|
|
|
|
|
String number = Integer.toString(myGame.getHeight() - y);
|
|
|
|
|
g.drawString(number,
|
|
|
|
|
BORDER_WIDTH - 10,
|
|
|
|
|
BORDER_WIDTH + Math.round(y*cellHeight) + Math.round(cellHeight/2) + 5);
|
|
|
|
|
g.drawString(number,
|
|
|
|
|
getWidth() - BORDER_WIDTH + 5,
|
|
|
|
|
BORDER_WIDTH + Math.round(y*cellHeight) + Math.round(cellHeight/2) + 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw inner border line
|
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
|
g.drawRect(BORDER_WIDTH - 1, BORDER_WIDTH - 1,
|
|
|
|
|
getWidth() - 2*BORDER_WIDTH + 1, getHeight() - 2*BORDER_WIDTH + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void drawPiece(Graphics g, Piece piece) {
|
|
|
|
|
g.drawImage(
|
|
|
|
|
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
|
|
|
|
|
MARGIN+(xCoordFromGame(piece.getX())),
|
|
|
|
|
MARGIN+(yCoordFromGame(piece.getY())),
|
|
|
|
|
BORDER_WIDTH + MARGIN + (xCoordFromGame(piece.getX())),
|
|
|
|
|
BORDER_WIDTH + MARGIN + (yCoordFromGame(piece.getY())),
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Image getChessPieceImageFromType(PieceType type, boolean isWhite) {
|
|
|
|
|
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
|
|
|
|
int y = PIECE_HEIGHT * (isWhite?1:0);
|
|
|
|
|
@ -164,33 +238,34 @@ public class JPanelChessBoard extends JPanel {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float cellWidth() {
|
|
|
|
|
return (float) this.getWidth()/ (float)myGame.getWidth();
|
|
|
|
|
return (float) (this.getWidth() - 2*BORDER_WIDTH) / (float)myGame.getWidth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float cellHeight() {
|
|
|
|
|
return (float)this.getHeight()/ (float)myGame.getHeight();
|
|
|
|
|
return (float)(this.getHeight() - 2*BORDER_WIDTH) / (float)myGame.getHeight();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int xCoordFromGame(int x) {
|
|
|
|
|
return Math.round(x*cellWidth());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int yCoordFromGame(int y) {
|
|
|
|
|
return Math.round(y*cellHeight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void togglePieceSelector() {
|
|
|
|
|
pieceSelectorMode = ! pieceSelectorMode;
|
|
|
|
|
pieceSelectorMode = !pieceSelectorMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void toggleAdderMode() {
|
|
|
|
|
pieceAdderMode = ! pieceAdderMode;
|
|
|
|
|
pieceAdderMode = !pieceAdderMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isPieceSelectorMode() {
|
|
|
|
|
return pieceSelectorMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isPieceAdderMode() {
|
|
|
|
|
return pieceAdderMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|