Add boardPanel to display chess board grid in GUI
parent
af8c068fce
commit
42bbdf3e43
|
|
@ -21,19 +21,45 @@ public class Gui {
|
|||
mainFrame();
|
||||
}
|
||||
|
||||
|
||||
public JFrame mainFrame() {
|
||||
JFrame frame = new JFrame();
|
||||
frame.setSize(1600, 1000);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.add(chessPanel());
|
||||
frame.add(statsPanel());
|
||||
frame.add(chessPanel(boardPanel()));
|
||||
|
||||
frame.setDefaultCloseOperation(2);
|
||||
frame.setVisible(true);
|
||||
return frame;
|
||||
}
|
||||
|
||||
public JPanel chessPanel() {
|
||||
public JPanel boardPanel() {
|
||||
JPanel boardPanel = new JPanel(new GridLayout(8, 8));
|
||||
boardPanel.setPreferredSize(new Dimension(800, 800));
|
||||
|
||||
for (int row = 0; row < 8; row++) {
|
||||
|
||||
for (int col = 0; col < 8; col++) {
|
||||
|
||||
JLabel label = new JLabel("", SwingConstants.CENTER);
|
||||
label.setOpaque(true);
|
||||
label.setFont(new Font("Serif", Font.BOLD, 40));
|
||||
if ((row + col) % 2 == 0) {
|
||||
label.setBackground(new Color(0x778da9));
|
||||
} else {
|
||||
label.setBackground(new Color(0xe0e1dd));
|
||||
}
|
||||
fields[row][col] = label; // <-- Save the label
|
||||
boardPanel.add(label);
|
||||
|
||||
}
|
||||
}
|
||||
boardPanel.setBackground(new Color(0x1b263b));
|
||||
return boardPanel;
|
||||
}
|
||||
|
||||
public JPanel chessPanel(JPanel panel) {
|
||||
JPanel chessPanel = new JPanel(new GridBagLayout());
|
||||
GridBagConstraints board = new GridBagConstraints();
|
||||
chessPanel.setBackground(new Color(0x1b263b));
|
||||
|
|
@ -44,6 +70,7 @@ public class Gui {
|
|||
board.insets = new Insets(0, 0, 0, 0);
|
||||
//oben, links, unten, rechts
|
||||
board.fill = GridBagConstraints.BOTH;
|
||||
chessPanel.add(panel);
|
||||
return chessPanel;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue