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 { public class ActionHandler implements ActionListener {
StartGameGui restartGame = new StartGameGui(); StartGameGui restartGame = new StartGameGui();
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == gui.CloseBtn) { if (e.getSource() == gui.CloseBtn) {
System.exit(0); System.exit(0);
} }
if (e.getSource() == GameGui.ClosingBtn) { if (e.getSource() == GameGui.ClosingBtn) {
System.exit(0); System.exit(0);
} }
if (e.getSource() == GameGui.RestartBtn) { if (e.getSource() == GameGui.RestartBtn) {
GameGui.questionNr = 0; GameGui.questionNr = 0;
GameGui.LosingPanel.setVisible(false); GameGui.LosingPanel.setVisible(false);
GameGui.counter = 1; GameGui.counter = 1;
restartGame.startGame(); restartGame.startGame();
} }
if (e.getSource() == GameGui.RestartBtn1) { if (e.getSource() == GameGui.RestartBtn1) {
GameGui.questionNr = 0; GameGui.questionNr = 0;
GameGui.WinningPanel.setVisible(false); GameGui.WinningPanel.setVisible(false);
GameGui.counter = 1; GameGui.counter = 1;
restartGame.startGame(); restartGame.startGame();
} }
} }
} }

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

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

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

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

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

@ -9,58 +9,55 @@ public class loadNextQuestion implements ActionListener {
GameGui gameStatus = new GameGui(); GameGui gameStatus = new GameGui();
runGame StartGame = new runGame(); runGame StartGame = new runGame();
@Override @Override
public void actionPerformed(ActionEvent e) { 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