231 lines
8.0 KiB
Java
231 lines
8.0 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
|
|
public class startLocalGame {
|
|
// Funktionshilfen
|
|
int semesterCounter = 1; // Semester Counter Label
|
|
String leftPlayerNickname = "Spieler 1";
|
|
String rightPlayerNickname = "Spieler 2";
|
|
|
|
// Grafiken
|
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
|
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
|
ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png");
|
|
|
|
// Frame
|
|
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 semesterlable = new JLabel("Semester");
|
|
JLabel leftPlayerName = new JLabel("Name");
|
|
JLabel rightPlayerName = new JLabel("KI-Level");
|
|
JLabel leftPlayerIcon = new JLabel(humanPlayerIcon);
|
|
JLabel rightPlayerIcon = new JLabel(aiPlayerIcon);
|
|
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
|
|
|
|
// Buttons
|
|
JButton backButton = new JButton(backButtonIcon);
|
|
JButton leftPlayerLeftButton = new JButton("<-");
|
|
JButton leftPlayerRightButton = new JButton("->");
|
|
JButton semesterUpButton = new JButton("^");
|
|
JButton semesterDownButton = new JButton("v");
|
|
JButton rightPlayerLeftButton = new JButton("<-");
|
|
JButton rightPlayerRightButton = new JButton("->");
|
|
JButton startButton = new JButton("Start!");
|
|
|
|
// Textfelder
|
|
JTextField leftPlayerTextField = new JTextField(20);
|
|
JTextField rightPlayerTextField = new JTextField(20);
|
|
|
|
startLocalGame() {
|
|
// Erstelle Frame
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
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);
|
|
|
|
// Erstelle Label
|
|
frameTitle.setBounds(20, 20, 200, 30);
|
|
frame.add(frameTitle);
|
|
|
|
semesterlable.setBounds(700, 300, 200, 30);
|
|
frame.add(semesterlable);
|
|
|
|
leftPlayerName.setBounds(50, 625, 200, 30);
|
|
frame.add(leftPlayerName);
|
|
|
|
rightPlayerName.setBounds(1200, 625, 200, 30);
|
|
frame.add(rightPlayerName);
|
|
|
|
leftPlayerIcon.setBounds(75, 400, 200, 128);
|
|
frame.add(leftPlayerIcon);
|
|
|
|
rightPlayerIcon.setBounds(1225, 400, 200, 128);
|
|
frame.add(rightPlayerIcon);
|
|
|
|
semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons
|
|
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
frame.add(semesterCounterLabel);
|
|
|
|
// Erstellt Buttons
|
|
backButton.setBounds(1380, 20, 80, 80);
|
|
frame.add(backButton);
|
|
|
|
leftPlayerLeftButton.setBounds(50, 450, 50, 50);
|
|
frame.add(leftPlayerLeftButton);
|
|
|
|
leftPlayerRightButton.setBounds(250, 450, 50, 50);
|
|
frame.add(leftPlayerRightButton);
|
|
|
|
semesterUpButton.setBounds(725, 400, 50, 50);
|
|
frame.add(semesterUpButton);
|
|
|
|
semesterDownButton.setBounds(725, 550, 50, 50);
|
|
frame.add(semesterDownButton);
|
|
|
|
rightPlayerLeftButton.setBounds(1200, 450, 50, 50);
|
|
frame.add(rightPlayerLeftButton);
|
|
|
|
rightPlayerRightButton.setBounds(1400, 450, 50, 50);
|
|
frame.add(rightPlayerRightButton);
|
|
|
|
startButton.setBounds(700, 750, 100, 50);
|
|
frame.add(startButton);
|
|
|
|
// Erstellt Textfelder
|
|
leftPlayerTextField.setBounds(50, 650, 250, 50);
|
|
frame.add(leftPlayerTextField);
|
|
|
|
rightPlayerTextField.setBounds(1200, 650, 250, 50);
|
|
frame.add(rightPlayerTextField);
|
|
|
|
//ZUSATZ
|
|
leftPlayerTextField.setText(leftPlayerNickname);
|
|
rightPlayerTextField.setText(rightPlayerNickname);
|
|
|
|
// Panels zum CardLayout hinzufügen
|
|
mainPanel.add(localGamePanel, "localGame");
|
|
|
|
// 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) {
|
|
semesterCounter++;
|
|
semesterCounterLabel.setText(String.valueOf(semesterCounter));
|
|
}
|
|
}
|
|
});
|
|
|
|
semesterDownButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (semesterCounter > 1) { // Verhindert, dass der Wert unter 1 fällt
|
|
semesterCounter--;
|
|
semesterCounterLabel.setText(String.valueOf(semesterCounter));
|
|
}
|
|
}
|
|
});
|
|
|
|
// PLAYERTOGGLEBUTTONS
|
|
leftPlayerLeftButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
toggleLeftPlayerIcon();
|
|
updateTextFields();
|
|
}
|
|
});
|
|
|
|
leftPlayerRightButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
toggleLeftPlayerIcon();
|
|
updateTextFields();
|
|
}
|
|
});
|
|
|
|
rightPlayerLeftButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
toggleRightPlayerIcon();
|
|
updateTextFields();
|
|
}
|
|
});
|
|
|
|
rightPlayerRightButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
toggleRightPlayerIcon();
|
|
updateTextFields();
|
|
}
|
|
});
|
|
|
|
frame.setVisible(true);
|
|
}
|
|
|
|
// TOGGLE METHODEN
|
|
private void toggleLeftPlayerIcon() {
|
|
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
|
leftPlayerIcon.setIcon(aiPlayerIcon);
|
|
} else {
|
|
leftPlayerIcon.setIcon(humanPlayerIcon);
|
|
}
|
|
}
|
|
|
|
private void toggleRightPlayerIcon() {
|
|
if (rightPlayerIcon.getIcon() == humanPlayerIcon) {
|
|
rightPlayerIcon.setIcon(aiPlayerIcon);
|
|
} else {
|
|
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;
|
|
}
|
|
}
|