board color
This commit is contained in:
parent
dee5ea30b1
commit
6b85a0b6e0
|
|
@ -22,8 +22,8 @@ public class JPanelChessBoard extends JPanel {
|
||||||
private Game myGame;
|
private Game myGame;
|
||||||
private MyInterface interfaceGlobal;
|
private MyInterface interfaceGlobal;
|
||||||
private BufferedImage spriteSheet;
|
private BufferedImage spriteSheet;
|
||||||
private int PIECE_WIDTH = 16; //in spritesheet
|
private int PIECE_WIDTH = 16;
|
||||||
private int PIECE_HEIGHT = 16; //in spritesheet
|
private int PIECE_HEIGHT = 16;
|
||||||
private int MARGIN = 6;
|
private int MARGIN = 6;
|
||||||
|
|
||||||
private boolean pieceSelectorMode;
|
private boolean pieceSelectorMode;
|
||||||
|
|
@ -47,24 +47,22 @@ public class JPanelChessBoard extends JPanel {
|
||||||
pieceAdderMode = false;
|
pieceAdderMode = false;
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent me) {
|
public void mousePressed(MouseEvent me) {
|
||||||
// System.out.println(me);
|
if (pieceSelectorMode) {
|
||||||
if(pieceSelectorMode) {
|
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 {
|
||||||
if(myGame == null) {
|
if (myGame == null) {
|
||||||
interfaceGlobal.instantiateSimu();
|
interfaceGlobal.instantiateSimu();
|
||||||
}
|
}
|
||||||
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
|
myGame.setPiece(selectedPieceIsWhite, selectedPieceType, x, y);
|
||||||
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
|
||||||
pieceAdderMode = false;
|
pieceAdderMode = false;
|
||||||
} else {
|
} else {
|
||||||
myGame.clickCoords(x,y);
|
myGame.clickCoords(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repaint();
|
repaint();
|
||||||
|
|
@ -72,7 +70,6 @@ public class JPanelChessBoard extends JPanel {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setGame(Game simu) {
|
public void setGame(Game simu) {
|
||||||
myGame = simu;
|
myGame = simu;
|
||||||
}
|
}
|
||||||
|
|
@ -81,116 +78,117 @@ public class JPanelChessBoard extends JPanel {
|
||||||
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(pieceSelectorMode) {
|
if (pieceSelectorMode) {
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
spriteSheet,
|
spriteSheet,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
Math.round(5*cellWidth()),
|
Math.round(5 * cellWidth()),
|
||||||
Math.round(2*cellHeight()),
|
Math.round(2 * cellHeight()),
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (myGame != null) {
|
if (myGame != null) {
|
||||||
// Draw Interface from state of simulator
|
|
||||||
float cellWidth = cellWidth();
|
float cellWidth = cellWidth();
|
||||||
float cellHeight = cellHeight();
|
float cellHeight = cellHeight();
|
||||||
|
|
||||||
g.setColor(Color.white);
|
Color lightColor = new Color(240, 217, 181); // beige
|
||||||
for(int x=0; x<myGame.getWidth();x++) {
|
Color darkColor = new Color(181, 136, 99); // brown
|
||||||
for (int y=0; y<myGame.getHeight(); y++) {
|
|
||||||
boolean isSelect = myGame.isSelected(x,y);
|
for (int x = 0; x < myGame.getWidth(); x++) {
|
||||||
boolean isHighlight = myGame.isHighlighted(x,y);
|
for (int y = 0; y < myGame.getHeight(); y++) {
|
||||||
if(isSelect) {
|
boolean isSelect = myGame.isSelected(x, y);
|
||||||
g.setColor(Color.blue);
|
boolean isHighlight = myGame.isHighlighted(x, y);
|
||||||
|
Color baseColor = ((x + y) % 2 == 0) ? lightColor : darkColor;
|
||||||
|
|
||||||
|
if (isSelect) {
|
||||||
|
baseColor = Color.blue;
|
||||||
}
|
}
|
||||||
if(isHighlight) {
|
if (isHighlight) {
|
||||||
g.setColor(Color.yellow);
|
baseColor = 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(baseColor);
|
||||||
|
g.fillRect(
|
||||||
|
Math.round(x * cellWidth),
|
||||||
|
Math.round(y * cellHeight),
|
||||||
|
Math.round(cellWidth),
|
||||||
|
Math.round(cellHeight)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
for (int y=0; y<myGame.getHeight(); y++) {
|
for (int y = 0; y < myGame.getHeight(); y++) {
|
||||||
int graphY = Math.round(y*cellHeight);
|
int graphY = Math.round(y * cellHeight);
|
||||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Piece piece : myGame.getPieces()) {
|
for (Piece piece : myGame.getPieces()) {
|
||||||
drawPiece(g,piece);
|
drawPiece(g, piece);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()),
|
||||||
MARGIN+(xCoordFromGame(piece.getX())),
|
MARGIN + (xCoordFromGame(piece.getX())),
|
||||||
MARGIN+(yCoordFromGame(piece.getY())),
|
MARGIN + (yCoordFromGame(piece.getY())),
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
|
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
|
||||||
return subImage.getScaledInstance(
|
return subImage.getScaledInstance(
|
||||||
Math.round(cellWidth())-2*MARGIN,
|
Math.round(cellWidth()) - 2 * MARGIN,
|
||||||
Math.round(cellHeight())-2*MARGIN, 0
|
Math.round(cellHeight()) - 2 * MARGIN,
|
||||||
);
|
0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int spriteSheetPositionOfPieceType(PieceType type) {
|
private int spriteSheetPositionOfPieceType(PieceType type) {
|
||||||
return 5-type.ordinal();
|
return 5 - type.ordinal();
|
||||||
}
|
}
|
||||||
|
|
||||||
private float cellWidth() {
|
private float cellWidth() {
|
||||||
return (float) this.getWidth()/ (float)myGame.getWidth();
|
return (float) this.getWidth() / (float) myGame.getWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
private float cellHeight() {
|
private float cellHeight() {
|
||||||
return (float)this.getHeight()/ (float)myGame.getHeight();
|
return (float) this.getHeight() / (float) myGame.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int xCoordFromGame(int x) {
|
private int xCoordFromGame(int x) {
|
||||||
return Math.round(x*cellWidth());
|
return Math.round(x * cellWidth());
|
||||||
}
|
}
|
||||||
|
|
||||||
private int yCoordFromGame(int y) {
|
private int yCoordFromGame(int y) {
|
||||||
return Math.round(y*cellHeight());
|
return Math.round(y * cellHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void togglePieceSelector() {
|
public void togglePieceSelector() {
|
||||||
pieceSelectorMode = ! pieceSelectorMode;
|
pieceSelectorMode = !pieceSelectorMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleAdderMode() {
|
public void toggleAdderMode() {
|
||||||
pieceAdderMode = ! pieceAdderMode;
|
pieceAdderMode = !pieceAdderMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPieceSelectorMode() {
|
public boolean isPieceSelectorMode() {
|
||||||
return pieceSelectorMode;
|
return pieceSelectorMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isPieceAdderMode() {
|
public boolean isPieceAdderMode() {
|
||||||
return pieceAdderMode;
|
return pieceAdderMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue