Added creation of new game tabs

- Switched to GridBagLayout in GamePane
- New games created in MenuBar are appended to the tabbed pane in
MainWindow
+ LogPanel class for displaying the game log inside a GamePane
- Removed LogFrame in favor of LogPanel
This commit is contained in:
Kai S. K. Engelbart 2019-09-01 12:45:06 +02:00
parent 513c0077e0
commit c712e066fe
4 changed files with 73 additions and 36 deletions

View File

@ -1,6 +1,7 @@
package dev.kske.chess.ui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
@ -28,16 +29,20 @@ public class GamePane extends JComponent {
private Color activeColor;
public GamePane() {
setLayout(new BorderLayout());
boardPane = new BoardPane();
add(boardPane, BorderLayout.CENTER);
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(); });
activeColor = Color.WHITE;
btnSwapColors = new JButton("Play as black");
btnSwapColors = new JButton("Play as black");
btnSwapColors.addActionListener((evt) -> {
game.swapColors();
btnSwapColors.setText("Play as " + activeColor.toString().toLowerCase());
@ -46,26 +51,57 @@ public class GamePane extends JComponent {
toolPanel.add(btnRestart);
toolPanel.add(btnSwapColors);
add(toolPanel, BorderLayout.NORTH);
JPanel letterPanel = new JPanel(new GridLayout(1, 8));
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);
}
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)));
add(numberPanel, BorderLayout.EAST);
// LogPanel logPanel = new LogPanel(game.getBoard().getLog());
// GridBagConstraints gbc_logPanel = new GridBagConstraints();
//
// 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 board pane
* @return The {@link Game} instance associated with this game pane
*/
public Game getGame() { return game; }

View File

@ -6,7 +6,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
@ -22,15 +21,14 @@ import dev.kske.chess.event.Subscribable;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>LogFrame.java</strong><br>
* File: <strong>LogPanel.java</strong><br>
* Created: <strong>17.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class LogFrame extends JFrame implements Subscribable {
public class LogPanel extends JPanel implements Subscribable {
private static final long serialVersionUID = 1932671698254197119L;
private JPanel mcontentPane;
private JTable mtable;
private Log log;
@ -38,20 +36,15 @@ public class LogFrame extends JFrame implements Subscribable {
/**
* Create the frame.
*/
public LogFrame(Log log) {
public LogPanel(Log log) {
this.log = log;
setTitle("Move History");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
mcontentPane = new JPanel();
mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
mcontentPane.setLayout(new BorderLayout(0, 0));
setContentPane(mcontentPane);
setBorder(new EmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(0, 0));
mtable = new JTable();
mtable.setEnabled(false);
mcontentPane.add(new JScrollPane(mtable), BorderLayout.CENTER);
add(new JScrollPane(mtable), BorderLayout.CENTER);
EventBus.getInstance().register(this);
handle(null);

View File

@ -51,7 +51,7 @@ public class MainWindow {
// Add frame content
tabbedPane = new JTabbedPane();
tabbedPane.add("Game 1", new GamePane());
addGamePane(); // Game is initialized inside MenuBar
frame.getContentPane().add(tabbedPane);
frame.setJMenuBar(new MenuBar(this));
@ -67,4 +67,15 @@ public class MainWindow {
* @return The currently selected {@link GamePane} component
*/
public GamePane getSelectedGamePane() { return (GamePane) tabbedPane.getSelectedComponent(); }
/**
* Creates a new {@link GamePane} and adds it to the tabbed pane.
*
* @return The new {@link GamePane}
*/
public GamePane addGamePane() {
GamePane gamePane = new GamePane();
tabbedPane.add("Game " + (tabbedPane.getComponentCount() + 1), gamePane);
return gamePane;
}
}

View File

@ -36,13 +36,16 @@ public class MenuBar extends JMenuBar {
newGameMenuItem.addActionListener((evt) -> {
GameConfigurationDialog dialog = new GameConfigurationDialog();
dialog.setVisible(true);
if (dialog.isStartGame()) startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(),
dialog.getWhiteName(), dialog.getBlackName()));
if (dialog.isStartGame()) {
GamePane gamePane = mainWindow.addGamePane();
gamePane.setGame(new Game(gamePane.getBoardPane(), dialog.getWhiteName(), dialog.getBlackName()));
}
});
gameMenu.add(newGameMenuItem);
add(gameMenu);
// TODO: Game initialization
// Start a game
startGame(new Game(mainWindow.getSelectedGamePane().getBoardPane(), "Natural Player", "Natural Player"));
}
@ -68,12 +71,6 @@ public class MenuBar extends JMenuBar {
private void initToolsMenu() {
JMenu toolsMenu = new JMenu("Tools");
// TODO: prevent multiple initializations
JMenuItem showLogMenuItem = new JMenuItem("Show move log");
showLogMenuItem.addActionListener(
(evt) -> new LogFrame(mainWindow.getSelectedGamePane().getGame().getBoard().getLog()).setVisible(true));
toolsMenu.add(showLogMenuItem);
JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN");
exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit()
.getSystemClipboard()