pretty cool
This commit is contained in:
123
app/src/main/java/chess/simplerender/Window.java
Normal file
123
app/src/main/java/chess/simplerender/Window.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package chess.simplerender;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
|
||||
public class Window extends JFrame {
|
||||
|
||||
private final JLabel cells[][];
|
||||
private final Game game;
|
||||
private final ChessBoard board;
|
||||
|
||||
private Coordinate lastClick = null;
|
||||
|
||||
public Window(Game game) {
|
||||
this.cells = new JLabel[8][8];
|
||||
this.game = game;
|
||||
this.board = game.getBoard();
|
||||
initSlots();
|
||||
build();
|
||||
setSize(800, 800);
|
||||
setVisible(true);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
}
|
||||
|
||||
private void initSlots() {
|
||||
this.game.OnRenderUpdate.connect(this::updateBoard);
|
||||
this.game.OnMoveRefused.connect(this::drawInvalid);
|
||||
}
|
||||
|
||||
private Color getCellColor(int x, int y) {
|
||||
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
||||
}
|
||||
|
||||
private void build() {
|
||||
JPanel content = new JPanel(new GridLayout(8, 8));
|
||||
setContentPane(content);
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel label = new JLabel();
|
||||
label.setOpaque(true);
|
||||
label.setBackground(getCellColor(x, y));
|
||||
this.cells[x][y] = label;
|
||||
|
||||
final int xx = x;
|
||||
final int yy = y;
|
||||
|
||||
label.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
onCellClicked(xx, yy);
|
||||
}
|
||||
});
|
||||
content.add(label);
|
||||
}
|
||||
}
|
||||
updateBoard();
|
||||
}
|
||||
|
||||
private void updateBoard() {
|
||||
PieceIcon pieceIcon = new PieceIcon();
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel cell = this.cells[x][y];
|
||||
cell.setIcon(pieceIcon.getIcon(this.board.pieceAt(new Coordinate(x, y))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void previewMoves(int x, int y) {
|
||||
boolean[][] allowedMoves = this.board.getAllowedMoves(new Coordinate(x, y));
|
||||
if (allowedMoves == null)
|
||||
return;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
JLabel cell = this.cells[i][j];
|
||||
if (allowedMoves[i][j])
|
||||
cell.setBackground(Color.CYAN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawInvalid(Move move) {
|
||||
JLabel from = this.cells[move.getStart().getX()][move.getStart().getY()];
|
||||
JLabel to = this.cells[move.getFinish().getX()][move.getFinish().getY()];
|
||||
from.setBackground(Color.RED);
|
||||
to.setBackground(Color.RED);
|
||||
}
|
||||
|
||||
private void clearMoves() {
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel cell = this.cells[x][y];
|
||||
cell.setBackground(getCellColor(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onCellClicked(int x, int y) {
|
||||
clearMoves();
|
||||
if (this.lastClick == null) {
|
||||
if (this.board.isCellEmpty(new Coordinate(x, y)))
|
||||
return;
|
||||
this.lastClick = new Coordinate(x, y);
|
||||
previewMoves(x, y);
|
||||
return;
|
||||
}
|
||||
if (!this.lastClick.equals(new Coordinate(x, y)))
|
||||
this.game.tryMove(new Move(lastClick, new Coordinate(x, y)));
|
||||
this.lastClick = null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user