GameBoard Klasse erstellt
parent
12d66335f6
commit
c626927a39
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue