From 42bbdf3e43f4f8339ddb512386575b939446ef7d Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 18 Jun 2025 17:28:55 +0200 Subject: [PATCH] Add boardPanel to display chess board grid in GUI --- .../hs_mannheim/informatik/chess/gui/Gui.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/gui/Gui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/gui/Gui.java index b8794d1..fe40b18 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/gui/Gui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/gui/Gui.java @@ -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; }