114 lines
3.1 KiB
Java
114 lines
3.1 KiB
Java
package dev.kske.chess.ui;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.EventQueue;
|
|
import java.awt.GridLayout;
|
|
import java.awt.Toolkit;
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JFrame;
|
|
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>MainWindow.java</strong><br>
|
|
* Created: <strong>01.07.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class MainWindow {
|
|
|
|
private JFrame mframe;
|
|
private JButton btnRestart, btnSwapColors;
|
|
private BoardPane boardPane;
|
|
private Game game;
|
|
private Color activeColor;
|
|
|
|
/**
|
|
* Launch the application.
|
|
*/
|
|
public static void main(String[] args) {
|
|
EventQueue.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
try {
|
|
MainWindow window = new MainWindow();
|
|
window.mframe.setVisible(true);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create the application.
|
|
*/
|
|
public MainWindow() {
|
|
initialize();
|
|
}
|
|
|
|
/**
|
|
* Initialize the contents of the frame.
|
|
*/
|
|
private void initialize() {
|
|
mframe = new JFrame("Chess by Kai S. K. Engelbart");
|
|
mframe.setResizable(false);
|
|
mframe.setBounds(100, 100, 494, 565);
|
|
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
mframe.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/pieces/queen_white.png")));
|
|
|
|
boardPane = new BoardPane();
|
|
mframe.getContentPane().add(boardPane, BorderLayout.CENTER);
|
|
|
|
JPanel toolPanel = new JPanel();
|
|
btnRestart = new JButton("Restart");
|
|
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
|
|
|
|
activeColor = Color.WHITE;
|
|
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);
|
|
mframe.getContentPane().add(toolPanel, BorderLayout.NORTH);
|
|
|
|
JPanel letterPanel = new JPanel(new GridLayout(1, 8));
|
|
for (int i = 0; i < 8; i++) {
|
|
JLabel letterLabel = new JLabel(String.valueOf((char) (65 + i)));
|
|
letterLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
letterPanel.add(letterLabel);
|
|
}
|
|
mframe.add(letterPanel, BorderLayout.SOUTH);
|
|
|
|
JPanel numberPanel = new JPanel(new GridLayout(8, 1));
|
|
for (int i = 0; i < 8; i++)
|
|
numberPanel.add(new JLabel(String.valueOf(8 - i)));
|
|
mframe.add(numberPanel, BorderLayout.EAST);
|
|
|
|
mframe.setJMenuBar(new MenuBar(this));
|
|
|
|
mframe.pack();
|
|
mframe.setLocationRelativeTo(null);
|
|
}
|
|
|
|
public BoardPane getBoardPane() { return boardPane; }
|
|
|
|
public Game getGame() { return game; }
|
|
|
|
public void setGame(Game game) {
|
|
if (this.game != null) this.game.disconnect();
|
|
this.game = game;
|
|
btnSwapColors.setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer
|
|
^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer);
|
|
}
|
|
}
|