Repository navigation
Expand file tree
/
Copy pathJavaFXXiangqiChessDisplay.java
More file actions
154 lines (135 loc) · 6.51 KB
/
Copy pathJavaFXXiangqiChessDisplay.java
File metadata and controls
154 lines (135 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
/**
*
* A class representative of a chess board display for Xiangqi
* @author Peter Schlueter
*
*/
public class JavaFXXiangqiChessDisplay implements JavaFXChessBoardDisplay
{
//Field storing the primary background color of a tile on the chessboard
private static Background lightGrayBackground = new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, new Insets(5)));
//Field storing the secondary (alternate) background color of a tile on the chessboard
private static Background darkGrayBackground = new Background(new BackgroundFill(Color.DARKGRAY, CornerRadii.EMPTY, new Insets(5)));
//Field storing the background color of a piece on the north side of the chessboard
private static BackgroundFill northPieceColor = new BackgroundFill(Color.RED, new CornerRadii(50), new Insets(5));
//Field storing the background color of a piece on the south side of the chessboard
private static BackgroundFill southPieceColor = new BackgroundFill(Color.SILVER, new CornerRadii(50), new Insets(5));
//Fiekd storing the background color of a piece on the west side of the chessboard
private static BackgroundFill westPieceColor = new BackgroundFill(Color.DEEPPINK, new CornerRadii(50), new Insets(5));
//Field storing the background color of a piece on the east side of the chessboard
private static BackgroundFill eastPieceColor = new BackgroundFill(Color.TURQUOISE, new CornerRadii(50), new Insets(5));
//Field storing the highlight color of the board display
private static Background highlightColor = new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY));
//TODO: Have diagonal lines run through edges of nine-point fortress
/**
* @see JavaFXChessBoardDisplay#displayEmptySquare
*/
public void displayEmptySquare(Button button, int row, int column)
{
button.setBackground(lightGrayBackground);
if (((row == 0) || (row == 1) || (row == 2) || (row == 7) || (row == 8) || (row == 9))
&& ((column == 3) || (column == 4) || (column == 5)))
{
button.setBackground(darkGrayBackground);
}
button.setText(null);
button.setGraphic(null);
}
/**
* The link to the picture from which all piece icons are derived from cna be found after the method description
* @see JavaFXChessBoardDisplay#displayFilledSquare
* @see <a href = "https://www.pinterest.com/pin/540220917777814056/"> All Xiangqi Piece Icons Image Source </a>
*/
public void displayFilledSquare(Button button, int row, int column, ChessPiece piece)
{
//BackgroundFill spaceColor stores the color of the background which the piece should have
BackgroundFill spaceColor = lightGrayBackground.getFills().get(0);
if (((row == 0) || (row == 1) || (row == 2) || (row == 7) || (row == 8) || (row == 9))
&& ((column == 3) || (column == 4) || (column == 5)))
{
spaceColor = darkGrayBackground.getFills().get(0);
}
//Set the Background of the piece on this button based on its side
switch (piece.getSide())
{
case NORTH:
button.setBackground(new Background(spaceColor, northPieceColor));
break;
case SOUTH:
button.setBackground(new Background(spaceColor, southPieceColor));
break;
case WEST:
button.setBackground(new Background(spaceColor, westPieceColor));
break;
default:
button.setBackground(new Background(spaceColor, eastPieceColor));
break;
}
/*
* Implemenation of Icons for JavaFX Xiangqi Display (EXTRA CREDIT FEATURE)
*/
//Image pieceImage stores new Image which will store the image data of this piece
Image pieceImage = (Image)piece.getIcon();
//Depending on the label of the piece, set the image of the icon appropriately
switch (piece.getLabel())
{
case "X":
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi King Piece.png") : new Image("Assets/Black Xiangqi King Piece.png"));
break;
case "G":
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi Guard Piece.png") : new Image("Assets/Black Xiangqi Guard Piece.png"));
break;
case "E":
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi Elephant Piece.png") : new Image("Assets/Black Xiangqi Elephant Piece.png"));
break;
case "H":
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi Horse Piece.png") : new Image("Assets/Black Xiangqi Horse Piece.png"));
break;
case "C":
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi Cannon Piece.png") : new Image("Assets/Black Xiangqi Cannon Piece.png"));
break;
case "R":
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi Rook Piece.png") : new Image("Assets/Black Xiangqi Rook Piece.png"));
break;
default:
pieceImage = (piece.getSide() == ChessGame.Side.NORTH ? new Image("Assets/Red Xiangqi Soldier Piece.png") : new Image("Assets/Black Xiangqi Soldier Piece.png"));
break;
}
//ImageView pieceImageView stores the image of this piece's icon
ImageView pieceImageView = new ImageView(pieceImage);
//Set the bounds of pieceImageView so that the icon of the piece and its label fit within the bounds of the button
pieceImageView.setFitHeight(button.getMinHeight() / 2);
pieceImageView.setFitWidth(button.getMinWidth() / 2);
//Set the icon/graphic of the piece to be the icon which represents the piece
button.setGraphic(pieceImageView);
button.setText(piece.getLabel());
//Finally, set the text of the button, which will appear next to the button's icon when the button is displayed
button.setText(piece.getLabel());
}
/**
* @see JavaFXChessBoardDisplay#highlightSquare
*/
public void highlightSquare(boolean highlight, Button button, int row, int column, ChessPiece piece)
{
if (highlight)
{
button.setBackground(highlightColor);
}
else if (piece == null)
{
displayEmptySquare(button, row, column);
}
else
{
displayFilledSquare(button, row, column, piece);
}
}
}