From 1c649cf3ee58ad0af94648a7ff76c8bb0c34e6b8 Mon Sep 17 00:00:00 2001 From: Ozan-Can Ekinci Date: Sun, 15 Jan 2023 20:55:13 +0100 Subject: [PATCH] refactoring: adjusted layout of classes --- src/main/java/Gui/ActionHandler.java | 10 +-- src/main/java/Gui/GameGui.java | 101 +++++++++++------------ src/main/java/Gui/GameGuiInterface.java | 4 +- src/main/java/game/loadNextQuestion.java | 89 ++++++++++---------- 4 files changed, 100 insertions(+), 104 deletions(-) diff --git a/src/main/java/Gui/ActionHandler.java b/src/main/java/Gui/ActionHandler.java index 2ee890f..d8fd0e0 100644 --- a/src/main/java/Gui/ActionHandler.java +++ b/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(); } - + } } diff --git a/src/main/java/Gui/GameGui.java b/src/main/java/Gui/GameGui.java index 5d27dc3..4222ad7 100644 --- a/src/main/java/Gui/GameGui.java +++ b/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); } - + } diff --git a/src/main/java/Gui/GameGuiInterface.java b/src/main/java/Gui/GameGuiInterface.java index 684d24b..5250685 100644 --- a/src/main/java/Gui/GameGuiInterface.java +++ b/src/main/java/Gui/GameGuiInterface.java @@ -5,9 +5,9 @@ public interface GameGuiInterface { void createGui(); void CreatePanel(); - + void createWinningPanel(); - + void createLosingPanel(); void createHeadline(); diff --git a/src/main/java/game/loadNextQuestion.java b/src/main/java/game/loadNextQuestion.java index 4dec11e..b2fafdd 100644 --- a/src/main/java/game/loadNextQuestion.java +++ b/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(); + } + +}