From c626927a39b892dad9df1cd30d602d1ed61b5b56 Mon Sep 17 00:00:00 2001 From: Vickvick2002 Date: Fri, 3 Jan 2025 13:45:27 +0100 Subject: [PATCH] GameBoard Klasse erstellt --- .../java/PR2/HitoriSpiel/GUI/GameBoard.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Hitori/src/main/java/PR2/HitoriSpiel/GUI/GameBoard.java diff --git a/Hitori/src/main/java/PR2/HitoriSpiel/GUI/GameBoard.java b/Hitori/src/main/java/PR2/HitoriSpiel/GUI/GameBoard.java new file mode 100644 index 0000000..e91fdf0 --- /dev/null +++ b/Hitori/src/main/java/PR2/HitoriSpiel/GUI/GameBoard.java @@ -0,0 +1,65 @@ +package PR2.HitoriSpiel.GUI; + +import PR2.HitoriSpiel.Domain.HitoriBoard; +import PR2.HitoriSpiel.Domain.HitoriCell; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class GameBoard extends JPanel { + + private final HitoriBoard board; // Verbindung zur Logik + + public GameBoard(HitoriBoard board) { + this.board = board; + setLayout(new GridLayout(board.getSize(), board.getSize())); + + // Buttons für jede Zelle erstellen + for (int i = 0; i < board.getSize(); i++) { + for (int j = 0; j < board.getSize(); j++) { + HitoriCell cell = board.getCell(i, j); + JButton button = createCellButton(cell, i, j); + add(button); + } + } + } + + private JButton createCellButton(HitoriCell cell, int row, int col) { + JButton button = new JButton(String.valueOf(cell.getNumber())); + updateButtonState(button, cell); + + // ActionListener für Benutzereinganen + button.addActionListener((ActionEvent e) -> { + toggleCellState(cell); + updateButtonState(button, cell); + }); + return button; + } + + private void toggleCellState(HitoriCell cell) { + if (cell.getState() == HitoriCell.CellState.GRAY) { + cell.setState(HitoriCell.CellState.BLACK); + } else if (cell.getState() == HitoriCell.CellState.BLACK) { + cell.setState(HitoriCell.CellState.WHITE); + } else { + cell.setState(HitoriCell.CellState.GRAY); + } + } + + private void updateButtonState(JButton button, HitoriCell cell) { + switch (cell.getState()) { + case GRAY: + button.setBackground(Color.LIGHT_GRAY); + break; + case BLACK: + button.setBackground(Color.BLACK); + button.setForeground(Color.WHITE); + break; + case WHITE: + button.setBackground(Color.WHITE); + button.setForeground(Color.BLACK); + break; + } + } +} \ No newline at end of file