From 05a4463ba83a49ae91f537dbcafa8fe9aa003785 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Mon, 3 Feb 2020 23:35:01 +0100 Subject: [PATCH] BreakOut 5.4 --- src/break_out/controller/Controller.java | 8 ++++-- src/break_out/model/Game.java | 2 +- src/break_out/model/Level.java | 2 +- src/break_out/view/StartScreen.java | 35 ++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/break_out/controller/Controller.java b/src/break_out/controller/Controller.java index 7f55d69..1f4f898 100644 --- a/src/break_out/controller/Controller.java +++ b/src/break_out/controller/Controller.java @@ -136,7 +136,7 @@ public class Controller implements ActionListener, KeyListener { case KeyEvent.VK_ESCAPE: // exit current level game.getLevel().setFinished(true); - toStartScreen(); + toStartScreen(game.getLevel().getScore()); break; } @@ -161,10 +161,14 @@ public class Controller implements ActionListener, KeyListener { /** * This method switches the view to the StartScreen view. + * @param score The player score */ - public void toStartScreen() { + public void toStartScreen(int score) { view.showScreen(StartScreen.class.getName()); view.getStartScreen().requestFocusInWindow(); + + view.getStartScreen().addScore(score); + view.getStartScreen().repaint(); } /** diff --git a/src/break_out/model/Game.java b/src/break_out/model/Game.java index 9eaecc8..f592f3b 100644 --- a/src/break_out/model/Game.java +++ b/src/break_out/model/Game.java @@ -114,7 +114,7 @@ public class Game { controller.toPlayground(); } else { // tells the controller to switch to the startScreen of the game - controller.toStartScreen(); + controller.toStartScreen(level.getScore()); } diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index 06c3560..8687fae 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -301,7 +301,7 @@ public class Level extends Thread implements ILevel { // level failed } else { setFinished(true); - game.getController().toStartScreen(); + game.getController().toStartScreen(getScore()); } } diff --git a/src/break_out/view/StartScreen.java b/src/break_out/view/StartScreen.java index 2c5be0f..97f7594 100644 --- a/src/break_out/view/StartScreen.java +++ b/src/break_out/view/StartScreen.java @@ -51,6 +51,11 @@ public class StartScreen extends JPanel { */ private JLabel error; + /** + * The scoreMenu + */ + private SectionPanel scoreMenu; + /** * The constructor needs a view @@ -125,16 +130,16 @@ public class StartScreen extends JPanel { */ private void initializeScoreMenu() { // The layout - SectionPanel scoreMenu = new SectionPanel(Color.WHITE); + scoreMenu = new SectionPanel(Color.WHITE); scoreMenu.shady = false; scoreMenu.setLayout(new MigLayout("", "10[center, grow, fill]10", "5[center]5")); - // adding the compoenents to the layout + // adding the components to the layout JLabel headline = new JLabel("Scores"); headline.setFont(new Font("Sans-serif", Font.PLAIN, 16)); headline.setHorizontalAlignment(SwingConstants.CENTER); - scoreMenu.add(headline, "cell 0 0, gaptop 5"); + scoreMenu.add(headline, "cell 0 0, gaptop 5, wrap"); add(scoreMenu, "cell 1 0, gapleft 5"); } @@ -200,4 +205,28 @@ public class StartScreen extends JPanel { error.setText(""); } + /** + * Adds a score to the scoreboard + * @param score + */ + public void addScore(int score) { + // adding the components to the layout + + // name + JLabel lName = new JLabel(getPlayersName()); + lName.setFont(new Font("Sans-serif", Font.PLAIN, 16)); + lName.setHorizontalAlignment(SwingConstants.CENTER); + scoreMenu.add(lName, "gaptop 5"); + + // spacing + JLabel space = new JLabel(); + scoreMenu.add(space); + + // score + JLabel lScore = new JLabel(String.valueOf(score)); + lScore.setFont(new Font("Sans-serif", Font.PLAIN, 16)); + lScore.setHorizontalAlignment(SwingConstants.CENTER); + scoreMenu.add(lScore, "gaptop 5, wrap"); + } + }