Browse Source

refactoring: adjusted layout of classes

master
Ozan-Can Ekinci 2 years ago
parent
commit
1c649cf3ee
  1. 10
      src/main/java/Gui/ActionHandler.java
  2. 101
      src/main/java/Gui/GameGui.java
  3. 4
      src/main/java/Gui/GameGuiInterface.java
  4. 89
      src/main/java/game/loadNextQuestion.java

10
src/main/java/Gui/ActionHandler.java

@ -6,30 +6,30 @@ import java.awt.event.ActionListener;
public class ActionHandler implements ActionListener {
StartGameGui restartGame = new StartGameGui();
public void actionPerformed(ActionEvent e) {
if (e.getSource() == gui.CloseBtn) {
System.exit(0);
}
if (e.getSource() == GameGui.ClosingBtn) {
System.exit(0);
}
if (e.getSource() == GameGui.RestartBtn) {
GameGui.questionNr = 0;
GameGui.LosingPanel.setVisible(false);
GameGui.counter = 1;
restartGame.startGame();
}
if (e.getSource() == GameGui.RestartBtn1) {
GameGui.questionNr = 0;
GameGui.WinningPanel.setVisible(false);
GameGui.counter = 1;
restartGame.startGame();
}
}
}

101
src/main/java/Gui/GameGui.java

@ -13,17 +13,16 @@ import javax.swing.JPanel;
import game.loadNextQuestion;
import game.runGame;
public class GameGui implements GameGuiInterface {
public static JPanel MainPanel, ButtonPanel, headingPanel, WinningPanel, LosingPanel;
public static JLabel Headline, QuestionLabel, WinningLabel, LosingLabel, ShowQuestionNr, HighScoreLabel;
public static JButton AnswersBtn1, AnswersBtn2, AnswersBtn3, AnswersBtn4, RestartBtn, RestartBtn1, ClosingBtn;
public static int questionNr = 0;
public static int counter = 1;
public static int highScore = 0;
GridLayout grid;
GridBagConstraints gbc;
public static JPanel MainPanel, ButtonPanel, headingPanel, WinningPanel, LosingPanel;
public static JLabel Headline, QuestionLabel, WinningLabel, LosingLabel, ShowQuestionNr, HighScoreLabel;
public static JButton AnswersBtn1, AnswersBtn2, AnswersBtn3, AnswersBtn4, RestartBtn, RestartBtn1, ClosingBtn;
public static int questionNr = 0;
public static int counter = 1;
public static int highScore = 0;
GridLayout grid;
GridBagConstraints gbc;
@Override
public void createGui() {
@ -35,31 +34,31 @@ GridBagConstraints gbc;
}
@Override
public void CreatePanel(){
public void CreatePanel() {
MainPanel = new JPanel();
ButtonPanel = new JPanel();
headingPanel = new JPanel();
grid = new GridLayout(2,1);
grid = new GridLayout(2, 1);
grid.setVgap(150);
headingPanel.setLayout(grid);
gui.Frame.add(MainPanel);
}
@Override
public void createLosingPanel() {
MainPanel.setVisible(false);
ButtonPanel.setVisible(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 200;
gbc.ipady = 100;
gbc.insets = new Insets(20, 0, 0, 0);
RestartBtn = new JButton("Neustarten");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 200;
gbc.ipady = 100;
gbc.insets = new Insets(20, 0, 0, 0);
RestartBtn = new JButton("Neustarten");
ClosingBtn = new JButton("Schliessen");
LosingPanel = new JPanel();
LosingPanel.setLayout(new GridBagLayout());
LosingLabel = new JLabel();
@ -67,10 +66,10 @@ GridBagConstraints gbc;
LosingLabel.setVerticalAlignment(JLabel.CENTER);
LosingLabel.setHorizontalAlignment(JLabel.CENTER);
LosingLabel.setFont(new Font("Serif", Font.BOLD, 28));
LosingPanel.add(LosingLabel,gbc);
LosingPanel.add(RestartBtn,gbc);
LosingPanel.add(ClosingBtn,gbc);
LosingPanel.add(LosingLabel, gbc);
LosingPanel.add(RestartBtn, gbc);
LosingPanel.add(ClosingBtn, gbc);
RestartBtn.addActionListener(new ActionHandler());
ClosingBtn.addActionListener(new ActionHandler());
gui.Frame.add(LosingPanel);
@ -80,39 +79,39 @@ GridBagConstraints gbc;
public void createWinningPanel() {
GameGui.MainPanel.setVisible(false);
GameGui.ButtonPanel.setVisible(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 200;
gbc.ipady = 100;
gbc.insets = new Insets(20, 0, 0, 0);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 200;
gbc.ipady = 100;
gbc.insets = new Insets(20, 0, 0, 0);
RestartBtn1 = new JButton("Nochmal Spielen");
ClosingBtn = new JButton("Schliessen");
WinningPanel = new JPanel();
WinningPanel.setLayout(new GridBagLayout());
WinningLabel = new JLabel("Du hast Gewonnen!");
WinningLabel.setVerticalAlignment(JLabel.CENTER);
WinningLabel.setHorizontalAlignment(JLabel.CENTER);
WinningLabel.setFont(new Font("Serif", Font.BOLD, 28));
WinningPanel.add(WinningLabel,gbc);
WinningPanel.add(RestartBtn1,gbc);
WinningPanel.add(ClosingBtn,gbc);
WinningPanel.add(WinningLabel, gbc);
WinningPanel.add(RestartBtn1, gbc);
WinningPanel.add(ClosingBtn, gbc);
RestartBtn1.addActionListener(new ActionHandler());
ClosingBtn.addActionListener(new ActionHandler());
gui.Frame.add(WinningPanel);
gui.Frame.add(WinningPanel);
}
@Override
public void createShowQuestion(int i) {
int numLines = runGame.CountFileLines("QandA/Money.txt");
String[] arrMoney = runGame.readFile("QandA/Money.txt", numLines);
String questionText = "Frage " + counter + ": " + arrMoney[i - 1] + " Euro";
counter++;
ShowQuestionNr.setText(questionText);
MainPanel.add(ShowQuestionNr);
}
@ -126,7 +125,7 @@ GridBagConstraints gbc;
Headline.setHorizontalAlignment(JLabel.CENTER);
Headline.setFont(new Font("Serif", Font.BOLD, 28));
}
@Override
public void createQuestionLabel() {
QuestionLabel = new JLabel("Questions");
@ -136,47 +135,47 @@ GridBagConstraints gbc;
QuestionLabel.setFont(new Font("Serif", Font.BOLD, 20));
headingPanel.add(QuestionLabel);
}
@Override
public void createLayout() {
ButtonPanel.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10,10,10,10);
gbc.insets = new Insets(10, 10, 10, 10);
gbc.ipadx = 300;
gbc.ipady = 100;
}
@Override
public void createButtons() {
public void createButtons() {
AnswersBtn1 = new JButton();
gbc.gridx = 0;
gbc.gridy = 0;
AnswersBtn1.addActionListener(new loadNextQuestion());
ButtonPanel.add(AnswersBtn1,gbc);
ButtonPanel.add(AnswersBtn1, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
AnswersBtn2 = new JButton();
AnswersBtn2.addActionListener(new loadNextQuestion());
ButtonPanel.add(AnswersBtn2,gbc);
ButtonPanel.add(AnswersBtn2, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
AnswersBtn3 = new JButton();
AnswersBtn3.addActionListener(new loadNextQuestion());
ButtonPanel.add(AnswersBtn3,gbc);
ButtonPanel.add(AnswersBtn3, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
AnswersBtn4 = new JButton();
ButtonPanel.add(AnswersBtn4,gbc);
ButtonPanel.add(AnswersBtn4, gbc);
ButtonPanel.setVisible(true);
AnswersBtn4.addActionListener(new loadNextQuestion());
MainPanel.add(ButtonPanel);
}
@Override
public void highScore() {
if (highScore < questionNr) {
@ -185,5 +184,5 @@ GridBagConstraints gbc;
HighScoreLabel = new JLabel("Dein Highscore ist: Frage " + highScore);
LosingPanel.add(HighScoreLabel);
}
}

4
src/main/java/Gui/GameGuiInterface.java

@ -5,9 +5,9 @@ public interface GameGuiInterface {
void createGui();
void CreatePanel();
void createWinningPanel();
void createLosingPanel();
void createHeadline();

89
src/main/java/game/loadNextQuestion.java

@ -9,58 +9,55 @@ public class loadNextQuestion implements ActionListener {
GameGui gameStatus = new GameGui();
runGame StartGame = new runGame();
@Override
public void actionPerformed(ActionEvent e) {
if (runGame.splitAns[4].equals("1")) {
if (e.getSource() == GameGui.AnswersBtn1) {
loadQuestions();
} else {
wrongAnswer();
}
}
else if (runGame.splitAns[4].equals("2")) {
if (e.getSource() == GameGui.AnswersBtn2) {
loadQuestions();
} else {
wrongAnswer();
}
}
else if (runGame.splitAns[4].equals("3")) {
if (e.getSource() == GameGui.AnswersBtn3) {
loadQuestions();
} else {
wrongAnswer();
}
if (runGame.splitAns[4].equals("1")) {
if (e.getSource() == GameGui.AnswersBtn1) {
loadQuestions();
} else {
wrongAnswer();
}
else if (runGame.splitAns[4].equals("4")) {
if (e.getSource() == GameGui.AnswersBtn4) {
loadQuestions();
} else {
wrongAnswer();
}
}
else if (runGame.splitAns[4].equals("2")) {
if (e.getSource() == GameGui.AnswersBtn2) {
loadQuestions();
} else {
wrongAnswer();
}
}
public void loadQuestions() {
if (GameGui.questionNr != runGame.arrQuestions.length - 1) {
StartGame.run(GameGui.questionNr);
gameStatus.createShowQuestion(GameGui.questionNr);
} else {
gameStatus.createWinningPanel();
}
else if (runGame.splitAns[4].equals("3")) {
if (e.getSource() == GameGui.AnswersBtn3) {
loadQuestions();
} else {
wrongAnswer();
}
public void wrongAnswer() {
gameStatus.createLosingPanel();
gameStatus.highScore();
}
else if (runGame.splitAns[4].equals("4")) {
if (e.getSource() == GameGui.AnswersBtn4) {
loadQuestions();
} else {
wrongAnswer();
}
}
}
public void loadQuestions() {
if (GameGui.questionNr != runGame.arrQuestions.length - 1) {
StartGame.run(GameGui.questionNr);
gameStatus.createShowQuestion(GameGui.questionNr);
} else {
gameStatus.createWinningPanel();
}
}
public void wrongAnswer() {
gameStatus.createLosingPanel();
gameStatus.highScore();
}
}
Loading…
Cancel
Save