StartMenu Code erweitert
parent
1b64e31951
commit
3b6e3a45a7
|
@ -5,6 +5,7 @@ import PR2.HitoriSpiel.Domain.HitoriSolutionLoader;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import javax.swing.JPanel;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -68,8 +69,8 @@ public class StartMenu extends JFrame {
|
||||||
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
|
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
|
||||||
System.out.println("Verfügbare Spielfelder: " + boardFileNames);
|
System.out.println("Verfügbare Spielfelder: " + boardFileNames);
|
||||||
|
|
||||||
if (boardFileNames.isEmpty()) {
|
if (boardFileNames == null || boardFileNames.isEmpty()) {
|
||||||
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Information", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,74 +92,23 @@ public class StartMenu extends JFrame {
|
||||||
if (option == JOptionPane.OK_OPTION) {
|
if (option == JOptionPane.OK_OPTION) {
|
||||||
String selectedFile = (String) boardSelector.getSelectedItem();
|
String selectedFile = (String) boardSelector.getSelectedItem();
|
||||||
System.out.println("Ausgewählte Datei: " + selectedFile);
|
System.out.println("Ausgewählte Datei: " + selectedFile);
|
||||||
|
|
||||||
if (selectedFile != null) {
|
if (selectedFile != null) {
|
||||||
try {
|
// Lade das ausgewählte Spielfeld
|
||||||
String resourcePath = "/persistent/Hitori_Spielfelder/" + selectedFile;
|
System.out.println("Ich bin drin.");
|
||||||
System.out.println("Lade Datei von Pfad: " + resourcePath);
|
loadAndDisplayBoard(selectedFile);
|
||||||
|
|
||||||
|
|
||||||
int[][] boardData = loadBoard(resourcePath);
|
|
||||||
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
|
|
||||||
|
|
||||||
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates); // Stelle sicher, dass die Lösung korrekt geladen wird
|
|
||||||
GameBoard gameBoard = new GameBoard(hitoriBoard);
|
|
||||||
|
|
||||||
loadGameBoard2(gameBoard, solutionCoordinates);
|
|
||||||
//loadGameBoard(boardData);
|
|
||||||
// addGameControls(gameBoard);
|
|
||||||
|
|
||||||
System.out.println("Verfügbare Spielfelder: " + boardFileNames);
|
|
||||||
System.out.println("Ausgewählte Datei: " + selectedFile);
|
|
||||||
System.out.println("Lade Datei von Pfad: " + resourcePath);
|
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception ex) {
|
|
||||||
System.err.println("Fehler beim Laden der Datei: " + ex.getMessage());
|
|
||||||
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + ex.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void randomBoard() {
|
private void randomBoard() {
|
||||||
// Lade alle verfügbaren Spielfeld-Dateien
|
|
||||||
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
|
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
|
||||||
|
if (boardFileNames == null || boardFileNames.isEmpty()) {
|
||||||
// Überprüfe, ob Dateien verfügbar sind
|
|
||||||
if (boardFileNames.isEmpty()) {
|
|
||||||
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Fehler", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wähle eine zufällige Datei
|
|
||||||
String randomFile = boardFileNames.get(new Random().nextInt(boardFileNames.size()));
|
String randomFile = boardFileNames.get(new Random().nextInt(boardFileNames.size()));
|
||||||
System.out.println("Zufällige Datei gewählt: " + randomFile);
|
System.out.println("Zufällige Datei gewählt: " + randomFile);
|
||||||
|
loadAndDisplayBoard(randomFile);
|
||||||
try {
|
|
||||||
// Lade das zufällige Spielfeld
|
|
||||||
String resourcePath = "/persistent/Hitori_Spielfelder/" + randomFile;
|
|
||||||
System.out.println("Lade Datei von Pfad: " + resourcePath);
|
|
||||||
|
|
||||||
InputStream inputStream = getClass().getResourceAsStream(resourcePath);
|
|
||||||
if (inputStream == null) {
|
|
||||||
throw new IOException("Ressourcendatei nicht gefunden: " + resourcePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Spielfeld-Daten laden
|
|
||||||
int[][] boardData = BoardLoader.loadBoard(resourcePath);
|
|
||||||
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
|
|
||||||
|
|
||||||
// Spielfeld erstellen und anzeigen
|
|
||||||
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates);
|
|
||||||
GameBoard gameBoard = new GameBoard(hitoriBoard);
|
|
||||||
loadGameBoard2(gameBoard, solutionCoordinates);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Fehler beim Laden des Spielfelds
|
|
||||||
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -174,7 +124,7 @@ public class StartMenu extends JFrame {
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadGameBoard2(GameBoard gameBoard,List<String> solutionCoordinates) {
|
private void loadGameBoard(GameBoard gameBoard, List<String> solutionCoordinates) {
|
||||||
mainPanel.removeAll();
|
mainPanel.removeAll();
|
||||||
mainPanel.setLayout(new BorderLayout());
|
mainPanel.setLayout(new BorderLayout());
|
||||||
mainPanel.add(gameBoard, BorderLayout.CENTER);
|
mainPanel.add(gameBoard, BorderLayout.CENTER);
|
||||||
|
@ -182,13 +132,25 @@ public class StartMenu extends JFrame {
|
||||||
mainPanel.repaint();
|
mainPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadGameBoard(int[][] boardData) {
|
private void loadAndDisplayBoard(String selectedFile) {
|
||||||
// Neues Panel für das Spielfeld
|
try {
|
||||||
mainPanel.removeAll();
|
String resourcePath = "/persistent/Hitori_Spielfelder/" + selectedFile;
|
||||||
mainPanel.setLayout(new BorderLayout());
|
System.out.println("Lade Datei von Pfad: " + resourcePath);
|
||||||
mainPanel.add(new GameBoard(new HitoriBoard(boardData, List.of())), BorderLayout.CENTER);
|
|
||||||
mainPanel.revalidate();
|
int[][] boardData = loadBoard(resourcePath);
|
||||||
mainPanel.repaint();
|
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
|
||||||
|
|
||||||
|
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates); // Stelle sicher, dass die Lösung korrekt geladen wird
|
||||||
|
GameBoard gameBoard = new GameBoard(hitoriBoard);
|
||||||
|
//GameBoard gameBoard = new GameBoard(new HitoriBoard(boardData, solutionCoordinates));
|
||||||
|
loadGameBoard(gameBoard, solutionCoordinates);
|
||||||
|
|
||||||
|
System.out.println("Ausgewählte Datei: " + selectedFile);
|
||||||
|
System.out.println("Lade Datei von Pfad: " + resourcePath);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue