Struktur nochmal verbessert
parent
8e92dc7af4
commit
93c193aede
|
@ -0,0 +1,51 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class GameBoardView extends JPanel {
|
||||
private final JButton[][] cells; // Spielfeld als Buttons
|
||||
|
||||
public GameBoardView(int rows, int cols) {
|
||||
this.setLayout(new GridLayout(rows, cols)); // Grid-Layout für das Spielfeld
|
||||
cells = new JButton[rows][cols];
|
||||
|
||||
// Spielfeld-Buttons initialisieren
|
||||
for (int row = 0; row < rows; row++) {
|
||||
for (int col = 0; col < cols; col++) {
|
||||
cells[row][col] = new JButton();
|
||||
cells[row][col].setBackground(Color.LIGHT_GRAY);
|
||||
int finalRow = row;
|
||||
int finalCol = col;
|
||||
|
||||
// ActionListener für Button-Klick
|
||||
cells[row][col].addActionListener(e -> toggleCellColor(finalRow, finalCol));
|
||||
this.add(cells[row][col]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Methode zum Umschalten der Zellenfarbe
|
||||
private void toggleCellColor(int row, int col) {
|
||||
JButton button = cells[row][col];
|
||||
if (button.getBackground() == Color.LIGHT_GRAY) {
|
||||
button.setBackground(Color.BLACK);
|
||||
} else if (button.getBackground() == Color.BLACK) {
|
||||
button.setBackground(Color.WHITE);
|
||||
} else {
|
||||
button.setBackground(Color.LIGHT_GRAY);
|
||||
}
|
||||
}
|
||||
|
||||
// Methode zum Aktualisieren des Spielfelds (optional)
|
||||
public void render(int[][] board) {
|
||||
for (int row = 0; row < board.length; row++) {
|
||||
for (int col = 0; col < board[row].length; col++) {
|
||||
cells[row][col].setText(String.valueOf(board[row][col]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class HitoriApp {
|
||||
public static void start() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
// Hauptfenster erstellen
|
||||
JFrame frame = new JFrame("Hitori - Das Spiel");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(400, 400);
|
||||
|
||||
// Startmenü hinzufügen
|
||||
StartMenu startMenu = new StartMenu(frame);
|
||||
frame.add(startMenu);
|
||||
|
||||
frame.setVisible(true);
|
||||
frame.setLocationRelativeTo(null);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class StartMenu extends JPanel {
|
||||
|
||||
public StartMenu(JFrame parentFrame) {
|
||||
// Layout auf FlowLayout setzen
|
||||
this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // Zentriert mit Abständen
|
||||
|
||||
// Buttons erstellen
|
||||
JButton continueButton = createButton("Spiel fortsetzen", 200, 30);
|
||||
JButton selectBoardButton = createButton("Spielfeld aussuchen", 200, 30);
|
||||
JButton randomBoardButton = createButton("Zufälliges Spielfeld", 200, 30);
|
||||
JButton highscoreButton = createButton("Highscore anzeigen", 200, 30);
|
||||
JButton exitButton = createButton("Spiel beenden", 200, 30);
|
||||
|
||||
// ActionListener hinzufügen
|
||||
continueButton.addActionListener(e -> showMessage("Spiel fortsetzen wird implementiert."));
|
||||
selectBoardButton.addActionListener(e -> showMessage("Spielfeld auswählen wird implementiert."));
|
||||
randomBoardButton.addActionListener(e -> showMessage("Zufälliges Spielfeld wird gestartet."));
|
||||
highscoreButton.addActionListener(e -> showMessage("Highscore-Liste wird angezeigt."));
|
||||
exitButton.addActionListener(e -> System.exit(0));
|
||||
|
||||
// Buttons zum Panel hinzufügen
|
||||
this.add(continueButton);
|
||||
this.add(selectBoardButton);
|
||||
this.add(randomBoardButton);
|
||||
this.add(highscoreButton);
|
||||
this.add(exitButton);
|
||||
}
|
||||
|
||||
// Methode zum Anzeigen von Nachrichten
|
||||
private void showMessage(String message) {
|
||||
JOptionPane.showMessageDialog(this, message, "Info", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
|
||||
// Methode zum Erstellen von Buttons mit fester Größe
|
||||
private JButton createButton(String text, int width, int height) {
|
||||
JButton button = new JButton(text);
|
||||
button.setPreferredSize(new Dimension(width, height)); // Feste Größe setzen
|
||||
return button;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
package PR2.HitoriSpiel.Main;
|
||||
|
||||
import PR2.HitoriSpiel.GUI.HitoriApp;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("Hiii");
|
||||
System.out.println("Hello world!");
|
||||
HitoriApp.start();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue