127 lines
4.2 KiB
Java
127 lines
4.2 KiB
Java
package dev.kske.chess.ui;
|
|
|
|
import java.awt.GridBagConstraints;
|
|
import java.awt.GridBagLayout;
|
|
import java.awt.GridLayout;
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JComponent;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JPanel;
|
|
|
|
import dev.kske.chess.board.Piece.Color;
|
|
import dev.kske.chess.game.Game;
|
|
import dev.kske.chess.game.NaturalPlayer;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>GamePane.java</strong><br>
|
|
* Created: <strong>23.08.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class GamePane extends JComponent {
|
|
|
|
private static final long serialVersionUID = 4349772338239617477L;
|
|
|
|
private JButton btnRestart, btnSwapColors;
|
|
private BoardPane boardPane;
|
|
private LogPanel logPanel;
|
|
private Game game;
|
|
private Color activeColor;
|
|
|
|
public GamePane() {
|
|
activeColor = Color.WHITE;
|
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
gridBagLayout.columnWidths = new int[] { 450, 1, 0 };
|
|
gridBagLayout.rowHeights = new int[] { 33, 267, 1, 0 };
|
|
gridBagLayout.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
|
|
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
|
|
setLayout(gridBagLayout);
|
|
|
|
JPanel toolPanel = new JPanel();
|
|
btnRestart = new JButton("Restart");
|
|
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
|
|
|
|
btnSwapColors = new JButton("Play as black");
|
|
btnSwapColors.addActionListener((evt) -> {
|
|
game.swapColors();
|
|
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
|
|
activeColor = activeColor.opposite();
|
|
});
|
|
|
|
toolPanel.add(btnRestart);
|
|
toolPanel.add(btnSwapColors);
|
|
|
|
GridBagConstraints gbc_toolPanel = new GridBagConstraints();
|
|
gbc_toolPanel.anchor = GridBagConstraints.NORTH;
|
|
gbc_toolPanel.fill = GridBagConstraints.HORIZONTAL;
|
|
gbc_toolPanel.gridx = 0;
|
|
gbc_toolPanel.gridy = 0;
|
|
add(toolPanel, gbc_toolPanel);
|
|
boardPane = new BoardPane();
|
|
GridBagConstraints gbc_boardPane = new GridBagConstraints();
|
|
gbc_boardPane.fill = GridBagConstraints.BOTH;
|
|
gbc_boardPane.gridx = 0;
|
|
gbc_boardPane.gridy = 1;
|
|
add(boardPane, gbc_boardPane);
|
|
|
|
JPanel numberPanel = new JPanel(new GridLayout(8, 1));
|
|
GridBagConstraints gbc_numberPanel = new GridBagConstraints();
|
|
gbc_numberPanel.anchor = GridBagConstraints.WEST;
|
|
gbc_numberPanel.fill = GridBagConstraints.VERTICAL;
|
|
gbc_numberPanel.gridx = 1;
|
|
gbc_numberPanel.gridy = 1;
|
|
add(numberPanel, gbc_numberPanel);
|
|
|
|
JPanel letterPanel = new JPanel(new GridLayout(1, 8));
|
|
GridBagConstraints gbc_letterPanel = new GridBagConstraints();
|
|
gbc_letterPanel.anchor = GridBagConstraints.NORTH;
|
|
gbc_letterPanel.fill = GridBagConstraints.HORIZONTAL;
|
|
gbc_letterPanel.gridx = 0;
|
|
gbc_letterPanel.gridy = 2;
|
|
add(letterPanel, gbc_letterPanel);
|
|
|
|
// Initialize board coordinates
|
|
for (int i = 0; i < 8; i++) {
|
|
numberPanel.add(new JLabel(String.valueOf(8 - i)));
|
|
JLabel letterLabel = new JLabel(String.valueOf((char) (65 + i)));
|
|
letterLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
letterPanel.add(letterLabel);
|
|
}
|
|
|
|
// Initialize LogPanel
|
|
logPanel = new LogPanel();
|
|
GridBagConstraints gbc_logPanel = new GridBagConstraints();
|
|
gbc_logPanel.anchor = GridBagConstraints.EAST;
|
|
gbc_logPanel.fill = GridBagConstraints.VERTICAL;
|
|
gbc_logPanel.gridx = 2;
|
|
gbc_logPanel.gridy = 1;
|
|
add(logPanel, gbc_logPanel);
|
|
}
|
|
|
|
/**
|
|
* @return The {@link BoardPane} instance associated with this game pane
|
|
*/
|
|
public BoardPane getBoardPane() { return boardPane; }
|
|
|
|
/**
|
|
* @return The {@link Game} instance associated with this game pane
|
|
*/
|
|
public Game getGame() { return game; }
|
|
|
|
/**
|
|
* Assigns a new {@link Game} instance to this game pane. If exactly one of the
|
|
* players is natural, color swapping functionality is enabled.
|
|
*
|
|
* @param game The {@link Game} to assign to this game pane.
|
|
*/
|
|
public void setGame(Game game) {
|
|
if (this.game != null) this.game.stop();
|
|
this.game = game;
|
|
btnSwapColors.setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer
|
|
^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer);
|
|
logPanel.setLog(game.getBoard().getLog());
|
|
}
|
|
}
|