Created GameBoard.
Added showPanel methods to pass parameters between methods.
This commit is contained in:
parent
88d016ed99
commit
2274a1e558
Binary file not shown.
After Width: | Height: | Size: 84 B |
Binary file not shown.
After Width: | Height: | Size: 171 B |
|
@ -1,3 +1,113 @@
|
|||
public class GameBoard {
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class GameBoard extends JPanel {
|
||||
// Funktionshilfen
|
||||
//int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel
|
||||
|
||||
// Grafiken
|
||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||
ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.png");
|
||||
ImageIcon gameBoardX = new ImageIcon("graphics/gameboardx.png");
|
||||
|
||||
// Labels
|
||||
JLabel frameTitle = new JLabel("GameBoard");
|
||||
JLabel kontextText = new JLabel("Beispielhafter Kontext-Text");
|
||||
//kontextText.setFont(new Font("Roboto", Font.BOLD, 24)); //TODO setFont fixen
|
||||
|
||||
JButton backButton = new JButton(backButtonIcon);
|
||||
// Eigene ModulButtons
|
||||
JButton leftPlayerModul1 = new JButton("Modul 1"); //TODO: Dynamische Namen durch abgleich mit Semester
|
||||
JButton leftPlayerModul2 = new JButton("Modul 2");
|
||||
JButton leftPlayerModul3 = new JButton("Modul 3");
|
||||
JButton leftPlayerModul4 = new JButton("Modul 4");
|
||||
JButton leftPlayerModul5 = new JButton("Modul 5");
|
||||
JButton leftPlayerModul6 = new JButton("Modul 6");
|
||||
JButton leftPlayerModul7 = new JButton("Reset");
|
||||
// Gegnerische ModulButtons
|
||||
JButton rightPlayerModul1 = new JButton("Modul 1");
|
||||
JButton rightPlayerModul2 = new JButton("Modul 2");
|
||||
JButton rightPlayerModul3 = new JButton("Modul 3");
|
||||
JButton rightPlayerModul4 = new JButton("Modul 4");
|
||||
JButton rightPlayerModul5 = new JButton("Modul 5");
|
||||
JButton rightPlayerModul6 = new JButton("Modul 6");
|
||||
JButton rightPlayerModul7 = new JButton("Bereit");
|
||||
|
||||
public void buildPanel(MainFrame frame, int semesterCounter) {
|
||||
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// Panel für das Kontext-Text-Feld
|
||||
JPanel headerPanel = new JPanel();
|
||||
headerPanel.setLayout(new BorderLayout());
|
||||
headerPanel.add(kontextText, BorderLayout.WEST);
|
||||
headerPanel.add(backButton, BorderLayout.EAST);
|
||||
|
||||
// Panel für die Buttons des linken Spielers (ganz links)
|
||||
JPanel leftButtonsPanel = new JPanel();
|
||||
leftButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
|
||||
leftButtonsPanel.add(leftPlayerModul1);
|
||||
leftButtonsPanel.add(leftPlayerModul2);
|
||||
leftButtonsPanel.add(leftPlayerModul3);
|
||||
leftButtonsPanel.add(leftPlayerModul4);
|
||||
leftButtonsPanel.add(leftPlayerModul5);
|
||||
leftButtonsPanel.add(leftPlayerModul6);
|
||||
leftButtonsPanel.add(leftPlayerModul7);
|
||||
|
||||
// Panel für die Buttons des rechten Spielers (ganz rechts)
|
||||
JPanel rightButtonsPanel = new JPanel();
|
||||
rightButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
|
||||
rightButtonsPanel.add(rightPlayerModul1);
|
||||
rightButtonsPanel.add(rightPlayerModul2);
|
||||
rightButtonsPanel.add(rightPlayerModul3);
|
||||
rightButtonsPanel.add(rightPlayerModul4);
|
||||
rightButtonsPanel.add(rightPlayerModul5);
|
||||
rightButtonsPanel.add(rightPlayerModul6);
|
||||
rightButtonsPanel.add(rightPlayerModul7);
|
||||
|
||||
// Spielfelder erstellen (eigenes und gegnerisches)
|
||||
int gridSize = 13 + semesterCounter; // Größe des Spielfelds
|
||||
JPanel ownBoardPanel = new JPanel(new GridLayout(gridSize, gridSize));
|
||||
JPanel opponentBoardPanel = new JPanel(new GridLayout(gridSize, gridSize));
|
||||
|
||||
// Buttons für das eigene Spielfeld hinzufügen
|
||||
for (int i = 0; i < gridSize; i++) {
|
||||
for (int j = 0; j < gridSize; j++) {
|
||||
ownBoardPanel.add(new JButton(gameBoardEmtpy));
|
||||
}
|
||||
}
|
||||
|
||||
// Buttons für das gegnerische Spielfeld hinzufügen
|
||||
for (int i = 0; i < gridSize; i++) {
|
||||
for (int j = 0; j < gridSize; j++) {
|
||||
opponentBoardPanel.add(new JButton(gameBoardEmtpy));
|
||||
}
|
||||
}
|
||||
|
||||
// Panel für beide Spielfelder (nebeneinander in der Mitte)
|
||||
JPanel centerPanel = new JPanel();
|
||||
centerPanel.setLayout(new GridLayout(1, 2, 20, 0)); // 2 Spielfelder nebeneinander, mit Abstand von 20 Pixeln
|
||||
centerPanel.add(ownBoardPanel);
|
||||
centerPanel.add(opponentBoardPanel);
|
||||
|
||||
// Panels dem Hauptlayout hinzufügen
|
||||
add(leftButtonsPanel, BorderLayout.WEST);
|
||||
add(rightButtonsPanel, BorderLayout.EAST);
|
||||
add(headerPanel, BorderLayout.NORTH);
|
||||
add(centerPanel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
GameBoard(MainFrame frame,int semesterCounter) {
|
||||
buildPanel(frame, semesterCounter);
|
||||
/*
|
||||
rightPlayerRightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
toggleRightPlayerIcon();
|
||||
updateTextFields();
|
||||
}
|
||||
}); */
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,17 @@ public class MainFrame extends JFrame {
|
|||
|
||||
private CardLayout cardLayout;
|
||||
private JPanel mainPanel;
|
||||
|
||||
// ---------- //
|
||||
// Diverse Hilfsvariablen (für Parameterübergabe etc.)
|
||||
// Von startMultiplayerGame an JoinGame
|
||||
int localMult;
|
||||
|
||||
// Von startLocalGame an GameBoard
|
||||
int semesterCounter;
|
||||
// ---------- //
|
||||
|
||||
|
||||
public MainFrame() {
|
||||
setTitle("Studium Versenken");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
@ -29,12 +38,15 @@ public class MainFrame extends JFrame {
|
|||
startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
|
||||
coinToss coinToss = new coinToss(this);
|
||||
//JoinGame joinGame = new JoinGame(this,localMult);
|
||||
//GameBoard gameBoard = new GameBoard(this, localMult);
|
||||
|
||||
mainPanel.add(mainMenuView, "MainMenu");
|
||||
mainPanel.add(localGame, "LocalGame");
|
||||
mainPanel.add(multiplayerGame, "MultiplayerGame");
|
||||
mainPanel.add(coinToss, "coinToss");
|
||||
//mainPanel.add(joinGame, "JoinGame");
|
||||
//mainPanel.add(gameBoard, "GameBoard");
|
||||
|
||||
// Hauptpanel in JFrame hinzufügen
|
||||
add(mainPanel);
|
||||
|
||||
|
@ -46,19 +58,33 @@ public class MainFrame extends JFrame {
|
|||
public void showPanel(String panelName) {
|
||||
cardLayout.show(mainPanel, panelName);
|
||||
}
|
||||
|
||||
public void showPanelExtra(String panelName,int num) {
|
||||
// --- ShowPanel der startMultiplayerGame Klasse
|
||||
public void showPanelSMG(String panelName, int num) {
|
||||
this.localMult = num;
|
||||
|
||||
if (!isPanelPresent(panelName)) {
|
||||
//if (!isPanelPresent(panelName)) { //TODO potentiell raus
|
||||
JoinGame joinGame = new JoinGame(this, localMult);
|
||||
mainPanel.add(joinGame, panelName); // Dynamically add the panel
|
||||
mainPanel.revalidate(); // Refresh the layout
|
||||
mainPanel.repaint();
|
||||
}
|
||||
//}
|
||||
|
||||
cardLayout.show(mainPanel, panelName); // Show the panel
|
||||
}
|
||||
// --- ShowPanel der startLocalGame Klasse
|
||||
public void showPanelSLG(String panelName,int semesterCounter) {
|
||||
this.semesterCounter = semesterCounter;
|
||||
|
||||
//if (!isPanelPresent(panelName)) { //TODO potentiell raus
|
||||
GameBoard gameBoard = new GameBoard(this, semesterCounter);
|
||||
mainPanel.add(gameBoard, panelName); // Dynamically add the panel
|
||||
mainPanel.revalidate(); // Refresh the layout
|
||||
mainPanel.repaint();
|
||||
//}
|
||||
|
||||
cardLayout.show(mainPanel, panelName); // Show the panel
|
||||
}
|
||||
/* TODO ist dies unnötig?
|
||||
private boolean isPanelPresent(String panelName) {
|
||||
for (Component component : mainPanel.getComponents()) {
|
||||
if (panelName.equals(mainPanel.getClientProperty("name"))) {
|
||||
|
@ -66,7 +92,7 @@ public class MainFrame extends JFrame {
|
|||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} */
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
|
|
|
@ -143,7 +143,7 @@ public class startLocalGame extends JPanel {
|
|||
|
||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
||||
|
||||
startButton.addActionListener(e -> frame.showPanel("coinToss")); // TODO ECHTE FUNKTION EINFÜGEN
|
||||
startButton.addActionListener(e -> frame.showPanelSLG("GameBoard", semesterCounter)); // TODO ECHTE FUNKTION EINFÜGEN
|
||||
}
|
||||
|
||||
private void toggleLeftPlayerIcon() {
|
||||
|
|
|
@ -106,8 +106,8 @@ public class startMultiplayerGame extends JPanel {
|
|||
// ActionListener für den "Back" Button, um zum vorherigen Panel zurückzukehren
|
||||
|
||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
||||
joinGameButton.addActionListener(e -> frame.showPanelExtra("JoinGame",1));
|
||||
createGameButton.addActionListener(e -> frame.showPanelExtra("JoinGame",0));
|
||||
joinGameButton.addActionListener(e -> frame.showPanelSMG("JoinGame",1));
|
||||
createGameButton.addActionListener(e -> frame.showPanelSMG("JoinGame",0));
|
||||
}
|
||||
|
||||
// TOGGLE METHODEN
|
||||
|
|
Loading…
Reference in New Issue