board color

This commit is contained in:
bensefia 2025-05-07 16:17:08 +02:00
parent dee5ea30b1
commit 6b85a0b6e0
1 changed files with 76 additions and 78 deletions

View File

@ -22,8 +22,8 @@ public class JPanelChessBoard extends JPanel {
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 PIECE_WIDTH = 16;
private int PIECE_HEIGHT = 16;
private int MARGIN = 6;
private boolean pieceSelectorMode;
@ -47,24 +47,22 @@ public class JPanelChessBoard extends JPanel {
pieceAdderMode = false;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
// System.out.println(me);
if(pieceSelectorMode) {
int x = Math.round(me.getX()/cellWidth());
selectedPieceType = PieceType.values()[5-x];
if (pieceSelectorMode) {
int x = Math.round(me.getX() / cellWidth());
selectedPieceType = PieceType.values()[5 - x];
selectedPieceIsWhite = (me.getY() > cellHeight());
pieceSelectorMode = false;
} else {
if(myGame == null) {
if (myGame == null) {
interfaceGlobal.instantiateSimu();
}
int x = (me.getX()*myGame.getWidth())/getWidth();
int y = (me.getY()*myGame.getHeight())/getHeight();
if(pieceAdderMode) {
//TODO
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
int x = (me.getX() * myGame.getWidth()) / getWidth();
int y = (me.getY() * myGame.getHeight()) / getHeight();
if (pieceAdderMode) {
myGame.setPiece(selectedPieceIsWhite, selectedPieceType, x, y);
pieceAdderMode = false;
} else {
myGame.clickCoords(x,y);
myGame.clickCoords(x, y);
}
}
repaint();
@ -72,7 +70,6 @@ public class JPanelChessBoard extends JPanel {
});
}
public void setGame(Game simu) {
myGame = simu;
}
@ -81,60 +78,59 @@ public class JPanelChessBoard extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.black);
if(pieceSelectorMode) {
if (pieceSelectorMode) {
g.drawImage(
spriteSheet,
0,
0,
Math.round(5*cellWidth()),
Math.round(2*cellHeight()),
Math.round(5 * cellWidth()),
Math.round(2 * cellHeight()),
null
);
return;
}
if (myGame != null) {
// Draw Interface from state of simulator
float cellWidth = cellWidth();
float cellHeight = cellHeight();
g.setColor(Color.white);
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);
if(isSelect) {
g.setColor(Color.blue);
Color lightColor = new Color(240, 217, 181); // beige
Color darkColor = new Color(181, 136, 99); // brown
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);
Color baseColor = ((x + y) % 2 == 0) ? lightColor : darkColor;
if (isSelect) {
baseColor = Color.blue;
}
if(isHighlight) {
g.setColor(Color.yellow);
if (isHighlight) {
baseColor = Color.yellow;
}
if((x+y)%2==1 || isSelect || isHighlight) {
g.setColor(baseColor);
g.fillRect(
Math.round(x*cellWidth),
Math.round(y*cellHeight),
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);
for (int x = 0; x < myGame.getWidth(); x++) {
int graphX = Math.round(x * cellWidth);
g.drawLine(graphX, 0, graphX, this.getHeight());
}
for (int y=0; y<myGame.getHeight(); y++) {
int graphY = Math.round(y*cellHeight);
for (int y = 0; y < myGame.getHeight(); y++) {
int graphY = Math.round(y * cellHeight);
g.drawLine(0, graphY, this.getWidth(), graphY);
}
for (Piece piece : myGame.getPieces()) {
drawPiece(g,piece);
drawPiece(g, piece);
}
}
}
@ -142,55 +138,57 @@ public class JPanelChessBoard extends JPanel {
private void drawPiece(Graphics g, Piece piece) {
g.drawImage(
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
MARGIN+(xCoordFromGame(piece.getX())),
MARGIN+(yCoordFromGame(piece.getY())),
MARGIN + (xCoordFromGame(piece.getX())),
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);
int x = spriteSheetPositionOfPieceType(type) * PIECE_WIDTH;
int y = PIECE_HEIGHT * (isWhite ? 1 : 0);
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
return subImage.getScaledInstance(
Math.round(cellWidth())-2*MARGIN,
Math.round(cellHeight())-2*MARGIN, 0
Math.round(cellWidth()) - 2 * MARGIN,
Math.round(cellHeight()) - 2 * MARGIN,
0
);
}
private int spriteSheetPositionOfPieceType(PieceType type) {
return 5-type.ordinal();
return 5 - type.ordinal();
}
private float cellWidth() {
return (float) this.getWidth()/ (float)myGame.getWidth();
return (float) this.getWidth() / (float) myGame.getWidth();
}
private float cellHeight() {
return (float)this.getHeight()/ (float)myGame.getHeight();
return (float) this.getHeight() / (float) myGame.getHeight();
}
private int xCoordFromGame(int x) {
return Math.round(x*cellWidth());
return Math.round(x * cellWidth());
}
private int yCoordFromGame(int y) {
return Math.round(y*cellHeight());
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;
}
}