Panellogik neu gemacht und dementsprechend views angepasst

This commit is contained in:
Kaver 2024-11-19 15:41:45 +01:00
parent 607004a3b3
commit a84110a8a8
5 changed files with 104 additions and 238 deletions

View File

@ -2,11 +2,12 @@ public class HalloSchiffeVersenken {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
//LUCAS CODE //LUCAS CODE
coinToss ct = new coinToss(); //coinToss ct = new coinToss();
//MainMenuModel model = new MainMenuModel(); //MainMenuModel model = new MainMenuModel();
//MainMenuView view = new MainMenuView(); //MainMenuView view = new MainMenuView(new MainFrame());
//MainMenuController controller = new MainMenuController(model, view); //MainMenuController controller = new MainMenuController(model, view);
MainFrame mf = new MainFrame();
mf.setVisible(true);
//System.out.println("HelloSchiffeVersenekn"); //System.out.println("HelloSchiffeVersenekn");

View File

@ -9,26 +9,26 @@ public class MainFrame extends JFrame {
public MainFrame() { public MainFrame() {
setTitle("Spiel UI mit CardLayout"); setTitle("Spiel UI mit CardLayout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600); setSize(1500, 1000);
setLocationRelativeTo(null);
// Erstelle das CardLayout und das Hauptpanel // CardLayout und Hauptpanel erstellen
cardLayout = new CardLayout(); cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout); mainPanel = new JPanel(cardLayout);
// Erstelle die verschiedenen Panels // Verschiedene Panels erstellen und hinzufügen
MainMenuPanel mainMenuPanel = new MainMenuPanel(this); MainMenuView mainMenuView = new MainMenuView(this);
LocalGamePanel localGamePanel = new LocalGamePanel(this); startLocalGame localGame = new startLocalGame(this);
MultiplayerGamePanel multiplayerGamePanel = new MultiplayerGamePanel(this); startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
// Füge die Panels dem CardLayout hinzu mainPanel.add(mainMenuView, "MainMenu");
mainPanel.add(mainMenuPanel, "MainMenu"); mainPanel.add(localGame, "LocalGame");
mainPanel.add(localGamePanel, "LocalGame"); mainPanel.add(multiplayerGame, "MultiplayerGame");
mainPanel.add(multiplayerGamePanel, "MultiplayerGame");
// Setze das Hauptpanel in das JFrame // Hauptpanel in JFrame hinzufügen
add(mainPanel); add(mainPanel);
// Zeige das Hauptmenü an // Hauptmenü anzeigen
cardLayout.show(mainPanel, "MainMenu"); cardLayout.show(mainPanel, "MainMenu");
} }

View File

@ -9,27 +9,27 @@ public class MainMenuController implements ActionListener {
public MainMenuController(MainMenuModel model, MainMenuView view) { public MainMenuController(MainMenuModel model, MainMenuView view) {
this.view = view; this.view = view;
this.model = model; this.model = model;
this.view.getLocalButton().addActionListener(this); //this.view.getLocalButton().addActionListener(this);
this.view.getMultiButton().addActionListener(this); //this.view.getMultiButton().addActionListener(this);
this.view.getSoundButton().addActionListener(this); // this.view.getSoundButton().addActionListener(this);
} }
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == view.getMultiButton()) { //if (e.getSource() == view.getMultiButton()) {
model.setGameMode("Multiplayer"); // model.setGameMode("Multiplayer");
MultiMenuModel multiModel = new MultiMenuModel(); // MultiMenuModel multiModel = new MultiMenuModel();
MultiMenuView multiView = new MultiMenuView(); // MultiMenuView multiView = new MultiMenuView();
MultiMenuController multiMenuController = new MultiMenuController(multiModel, multiView); // MultiMenuController multiMenuController = new MultiMenuController(multiModel, multiView);
view.addPanel(multiView.getMultiPanel(), "MultiMenu"); // view.addPanel(multiView.getMultiPanel(), "MultiMenu");
view.showPanel("MultiMenu"); // view.showPanel("MultiMenu");
}else if (e.getSource() == view.getSoundButton()) { // }else if (e.getSource() == view.getSoundButton()) {
view.toggleMute(); // view.toggleMute();
}else if(e.getSource() == view.getLocalButton()) { // }else if(e.getSource() == view.getLocalButton()) {
model.setGameMode("LocalGame"); model.setGameMode("LocalGame");
startLocalGame localGame = new startLocalGame(); startLocalGame localGame = new startLocalGame();
view.addPanel(localGame.getLocalPanel(), "LocalMenu"); // view.addPanel(localGame.getLocalPanel(), "LocalMenu");
view.showPanel("LocalMenu"); // view.showPanel("LocalMenu");
} }
} }
} //}

View File

@ -1,97 +1,51 @@
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.*; import javax.swing.*;
public class MainMenuView { public class MainMenuView extends JPanel {
private JPanel mainPanel = new JPanel(new CardLayout()); // CardLayout for switching
private JPanel menuPanel = new JPanel(null);
ImageIcon SoundIcon = new ImageIcon("graphics/sound button.png");
private JFrame frame = new JFrame();
private JLabel titelLabel = new JLabel("Studium versenken"); private JLabel titelLabel = new JLabel("Studium versenken");
private JButton lokalButton = new JButton("Lokal"); private JButton lokalButton = new JButton("Lokal");
private JButton multiButton= new JButton("Multiplayer"); private JButton multiButton = new JButton("Multiplayer");
private JButton soundButton = new JButton(SoundIcon); private JButton soundButton;
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
public MainMenuView() { Font robotoFont = new Font("Roboto", Font.BOLD, 45);
buildFrame(); private ImageIcon soundIcon = new ImageIcon("graphics/sound button.png");
buildPanel(); private ImageIcon muteIcon = new ImageIcon("graphics/sound button muted.png");
public MainMenuView(MainFrame frame) {
setLayout(null);
buildPanel(frame);
} }
public void buildPanel(){ private void buildPanel(MainFrame frame) {
mainPanel.add(menuPanel, "MainMenu");
lokalButton.setBounds(200, 200, 500, 500); lokalButton.setBounds(200, 200, 500, 500);
multiButton.setBounds(800, 200, 500, 500); multiButton.setBounds(800, 200, 500, 500);
titelLabel.setBounds(550, 10, 700, 100); titelLabel.setBounds(550, 10, 700, 100);
soundButton = new JButton(soundIcon);
soundButton.setBounds(20, 20, 128, 128); soundButton.setBounds(20, 20, 128, 128);
lokalButton.setOpaque(true);
multiButton.setOpaque(true);
titelLabel.setOpaque(true);
soundButton.setOpaque(true);
titelLabel.setFont(robotoFont); titelLabel.setFont(robotoFont);
lokalButton.setFont(robotoFont.deriveFont(50f)); lokalButton.setFont(robotoFont.deriveFont(50f));
multiButton.setFont(robotoFont.deriveFont(50f)); multiButton.setFont(robotoFont.deriveFont(50f));
menuPanel.add(titelLabel); add(titelLabel);
menuPanel.add(lokalButton); add(lokalButton);
menuPanel.add(multiButton); add(multiButton);
menuPanel.add(soundButton); add(soundButton);
//mainPanel.add(menuPanel, "MainMenu"); // Event Listener für Buttons
lokalButton.addActionListener(e -> frame.showPanel("LocalGame"));
multiButton.addActionListener(e -> frame.showPanel("MultiplayerGame"));
soundButton.addActionListener(e -> toggleMute());
} }
public void showPanel(String panelName) { private void toggleMute() {
CardLayout cl = (CardLayout)mainPanel.getLayout(); if (soundButton.getIcon() == soundIcon) {
cl.show(mainPanel, panelName); soundButton.setIcon(muteIcon);
} } else {
soundButton.setIcon(soundIcon);
public void addPanel(JPanel panel, String name) {
mainPanel.add(panel, name); // Neue Panels hinzufuegen
}
public void buildFrame() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1500, 1000);
frame.setLayout(null);
JLabel backgroundLabel = new JLabel(new ImageIcon("graphics/mainmenubackground.png"));
backgroundLabel.setBounds(0, 0, 1500, 1000);
mainPanel.setBounds(0, 0, 1500, 1000);
// mainPanel.setOpaque(true);
frame.setContentPane(new JPanel(null));
frame.getContentPane().add(backgroundLabel);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public void toggleMute(){
ImageIcon MuteIcon = new ImageIcon("graphics/sound button muted.png");
if(soundButton.getIcon()==SoundIcon) {
soundButton.setIcon(MuteIcon);
}else{
soundButton.setIcon(SoundIcon);
} }
} }
public JFrame getFrame() {
return frame;
}
public JButton getLocalButton() {
return lokalButton;
}
public JButton getMultiButton() {
return multiButton;
}
public JButton getSoundButton() {
return soundButton;
}
} }

View File

@ -14,24 +14,15 @@ public class startLocalGame {
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png"); ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png"); ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png");
// Frame // Labels und Buttons
JFrame frame = new JFrame("Lokales Spiel");
JPanel mainPanel = new JPanel(new CardLayout());
// Panels
JPanel localGamePanel = new JPanel(); // Panel für das lokale Spiel
startMultiplayerGame multiplayerGame = new startMultiplayerGame(); // Instanziere Multiplayer-Klasse
// Labels
JLabel frameTitle = new JLabel("Lokales Spiel"); JLabel frameTitle = new JLabel("Lokales Spiel");
JLabel semesterlable = new JLabel("Semester"); JLabel semesterLabel = new JLabel("Semester");
JLabel leftPlayerName = new JLabel("Name"); JLabel leftPlayerName = new JLabel("Name");
JLabel rightPlayerName = new JLabel("KI-Level"); JLabel rightPlayerName = new JLabel("KI-Level");
JLabel leftPlayerIcon = new JLabel(humanPlayerIcon); JLabel leftPlayerIcon = new JLabel(humanPlayerIcon);
JLabel rightPlayerIcon = new JLabel(aiPlayerIcon); JLabel rightPlayerIcon = new JLabel(aiPlayerIcon);
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter)); JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
// Buttons
JButton backButton = new JButton(backButtonIcon); JButton backButton = new JButton(backButtonIcon);
JButton leftPlayerLeftButton = new JButton("<-"); JButton leftPlayerLeftButton = new JButton("<-");
JButton leftPlayerRightButton = new JButton("->"); JButton leftPlayerRightButton = new JButton("->");
@ -41,157 +32,97 @@ public class startLocalGame {
JButton rightPlayerRightButton = new JButton("->"); JButton rightPlayerRightButton = new JButton("->");
JButton startButton = new JButton("Start!"); JButton startButton = new JButton("Start!");
// Textfelder
JTextField leftPlayerTextField = new JTextField(20); JTextField leftPlayerTextField = new JTextField(20);
JTextField rightPlayerTextField = new JTextField(20); JTextField rightPlayerTextField = new JTextField(20);
startLocalGame() { // Methode zur Erstellung des Panels
// Erstelle Frame public JPanel createLocalGamePanel(CardLayout cardLayout, JPanel mainPanel) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel localGamePanel = new JPanel();
frame.setSize(1500, 1000);
frame.setLayout(new BorderLayout()); // Haupt-Layout
frame.add(mainPanel, BorderLayout.CENTER);
// Layout Manager (absolute positionierung)
frame.setLayout(null);
// Lokales Spiel Panel erstellen
localGamePanel.setLayout(null); localGamePanel.setLayout(null);
// Erstelle Label // Setze Komponentenpositionen
frameTitle.setBounds(20, 20, 200, 30); frameTitle.setBounds(20, 20, 200, 30);
frame.add(frameTitle); localGamePanel.add(frameTitle);
semesterlable.setBounds(700, 300, 200, 30); semesterLabel.setBounds(700, 300, 200, 30);
frame.add(semesterlable); localGamePanel.add(semesterLabel);
leftPlayerName.setBounds(50, 625, 200, 30); leftPlayerName.setBounds(50, 625, 200, 30);
frame.add(leftPlayerName); localGamePanel.add(leftPlayerName);
rightPlayerName.setBounds(1200, 625, 200, 30); rightPlayerName.setBounds(1200, 625, 200, 30);
frame.add(rightPlayerName); localGamePanel.add(rightPlayerName);
leftPlayerIcon.setBounds(75, 400, 200, 128); leftPlayerIcon.setBounds(75, 400, 200, 128);
frame.add(leftPlayerIcon); localGamePanel.add(leftPlayerIcon);
rightPlayerIcon.setBounds(1225, 400, 200, 128); rightPlayerIcon.setBounds(1225, 400, 200, 128);
frame.add(rightPlayerIcon); localGamePanel.add(rightPlayerIcon);
semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons semesterCounterLabel.setBounds(725, 475, 50, 50);
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER); semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(semesterCounterLabel); localGamePanel.add(semesterCounterLabel);
// Erstellt Buttons
backButton.setBounds(1380, 20, 80, 80); backButton.setBounds(1380, 20, 80, 80);
frame.add(backButton); localGamePanel.add(backButton);
leftPlayerLeftButton.setBounds(50, 450, 50, 50); leftPlayerLeftButton.setBounds(50, 450, 50, 50);
frame.add(leftPlayerLeftButton); localGamePanel.add(leftPlayerLeftButton);
leftPlayerRightButton.setBounds(250, 450, 50, 50); leftPlayerRightButton.setBounds(250, 450, 50, 50);
frame.add(leftPlayerRightButton); localGamePanel.add(leftPlayerRightButton);
semesterUpButton.setBounds(725, 400, 50, 50); semesterUpButton.setBounds(725, 400, 50, 50);
frame.add(semesterUpButton); localGamePanel.add(semesterUpButton);
semesterDownButton.setBounds(725, 550, 50, 50); semesterDownButton.setBounds(725, 550, 50, 50);
frame.add(semesterDownButton); localGamePanel.add(semesterDownButton);
rightPlayerLeftButton.setBounds(1200, 450, 50, 50); rightPlayerLeftButton.setBounds(1200, 450, 50, 50);
frame.add(rightPlayerLeftButton); localGamePanel.add(rightPlayerLeftButton);
rightPlayerRightButton.setBounds(1400, 450, 50, 50); rightPlayerRightButton.setBounds(1400, 450, 50, 50);
frame.add(rightPlayerRightButton); localGamePanel.add(rightPlayerRightButton);
startButton.setBounds(700, 750, 100, 50); startButton.setBounds(700, 750, 100, 50);
frame.add(startButton); localGamePanel.add(startButton);
// Erstellt Textfelder
leftPlayerTextField.setBounds(50, 650, 250, 50); leftPlayerTextField.setBounds(50, 650, 250, 50);
frame.add(leftPlayerTextField); leftPlayerTextField.setText(leftPlayerNickname);
localGamePanel.add(leftPlayerTextField);
rightPlayerTextField.setBounds(1200, 650, 250, 50); rightPlayerTextField.setBounds(1200, 650, 250, 50);
frame.add(rightPlayerTextField);
//ZUSATZ
leftPlayerTextField.setText(leftPlayerNickname);
rightPlayerTextField.setText(rightPlayerNickname); rightPlayerTextField.setText(rightPlayerNickname);
localGamePanel.add(rightPlayerTextField);
// Panels zum CardLayout hinzufügen // ActionListener für Buttons
mainPanel.add(localGamePanel, "localGame"); semesterUpButton.addActionListener(e -> {
// Multiplayer-Panel erstellen und hinzufügen
JPanel multiplayerGamePanel = multiplayerGame.createMultiplayerPanel((CardLayout) mainPanel.getLayout(), mainPanel);
mainPanel.add(multiplayerGamePanel, "multiplayerGame");
// ActionListener für den Start-Button
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Wechsel zum Multiplayer-Panel
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, "multiplayerGame");
}
});
// SEMESTERBUTTONS
semesterUpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (semesterCounter < 6) { if (semesterCounter < 6) {
semesterCounter++; semesterCounter++;
semesterCounterLabel.setText(String.valueOf(semesterCounter)); semesterCounterLabel.setText(String.valueOf(semesterCounter));
} }
}
}); });
semesterDownButton.addActionListener(new ActionListener() { semesterDownButton.addActionListener(e -> {
@Override if (semesterCounter > 1) {
public void actionPerformed(ActionEvent e) {
if (semesterCounter > 1) { // Verhindert, dass der Wert unter 1 fällt
semesterCounter--; semesterCounter--;
semesterCounterLabel.setText(String.valueOf(semesterCounter)); semesterCounterLabel.setText(String.valueOf(semesterCounter));
} }
}
}); });
// PLAYERTOGGLEBUTTONS leftPlayerLeftButton.addActionListener(e -> toggleLeftPlayerIcon());
leftPlayerLeftButton.addActionListener(new ActionListener() { leftPlayerRightButton.addActionListener(e -> toggleLeftPlayerIcon());
@Override
public void actionPerformed(ActionEvent e) {
toggleLeftPlayerIcon();
updateTextFields();
}
});
leftPlayerRightButton.addActionListener(new ActionListener() { rightPlayerLeftButton.addActionListener(e -> toggleRightPlayerIcon());
@Override rightPlayerRightButton.addActionListener(e -> toggleRightPlayerIcon());
public void actionPerformed(ActionEvent e) {
toggleLeftPlayerIcon();
updateTextFields();
}
});
rightPlayerLeftButton.addActionListener(new ActionListener() { backButton.addActionListener(e -> cardLayout.show(mainPanel, "MainMenu"));
@Override
public void actionPerformed(ActionEvent e) {
toggleRightPlayerIcon();
updateTextFields();
}
});
rightPlayerRightButton.addActionListener(new ActionListener() { startButton.addActionListener(e -> cardLayout.show(mainPanel, "multiplayerGame"));
@Override
public void actionPerformed(ActionEvent e) {
toggleRightPlayerIcon();
updateTextFields();
}
});
frame.setVisible(true); return localGamePanel;
} }
// TOGGLE METHODEN
private void toggleLeftPlayerIcon() { private void toggleLeftPlayerIcon() {
if (leftPlayerIcon.getIcon() == humanPlayerIcon) { if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
leftPlayerIcon.setIcon(aiPlayerIcon); leftPlayerIcon.setIcon(aiPlayerIcon);
@ -207,24 +138,4 @@ public class startLocalGame {
rightPlayerIcon.setIcon(humanPlayerIcon); rightPlayerIcon.setIcon(humanPlayerIcon);
} }
} }
// Methode zum Aktualisieren der Textfelder basierend auf den ausgewählten Icons
private void updateTextFields() {
// Linker Spieler
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
leftPlayerTextField.setText(leftPlayerNickname);
} else {
leftPlayerTextField.setText("Leicht");
}
// Rechter Spieler
if (rightPlayerIcon.getIcon() == humanPlayerIcon) {
rightPlayerTextField.setText(rightPlayerNickname);
} else {
rightPlayerTextField.setText("Leicht");
}
}
public JPanel getLocalPanel() {
return localGamePanel;
}
} }