diff --git a/src/break_out/Constants.java b/src/break_out/Constants.java index 5c321b0..75ce1ac 100644 --- a/src/break_out/Constants.java +++ b/src/break_out/Constants.java @@ -94,4 +94,29 @@ public class Constants { */ public static final Color COLOR_PADDLE_TOP = new Color(140, 226, 165); + /** + * The component color for the text + */ + public static final Color COLOR_TEXT = new Color(0, 0, 0); + + /** + * The component color for the scoreboard + */ + public static final Color COLOR_SCOREBOARD = new Color(140, 140, 140, 144); + + /** + * The padding / offset of the scoreboard to the inner text elements + */ + public static final int SCOREBOARD_OFFSET = 15; + + /** + * The string for the score on the scoreboard + */ + public static final String SCOREBOARD_SCORE = "Score: "; + + /** + * The string for the lives on the scoreboard + */ + public static final String SCOREBOARD_LIVES = "Lives: "; + } \ No newline at end of file diff --git a/src/break_out/view/Field.java b/src/break_out/view/Field.java index 6c57969..c9093ec 100644 --- a/src/break_out/view/Field.java +++ b/src/break_out/view/Field.java @@ -1,11 +1,15 @@ package break_out.view; import java.awt.Color; -import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Dimension; import java.awt.RenderingHints; +import java.awt.Font; +import java.awt.font.TextAttribute; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import javax.swing.JPanel; @@ -114,6 +118,9 @@ public class Field extends JPanel { // Calls the method for drawing the top paddle drawPaddleTop(g2); + // Calls the method for drawing the scoreboard + drawScoreboard(g2); + } /** @@ -249,4 +256,91 @@ public class Field extends JPanel { } } + /** + * Draws the scoreboard with score and lives + * @param g2 + */ + private void drawScoreboard(Graphics2D g2) { + Font currentFont = g2.getFont(); + + // source: Stackoverflow Question - 18249592 + Map attributes = new HashMap<>(); + attributes.put(TextAttribute.FAMILY, currentFont.getFamily()); + attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); + attributes.put(TextAttribute.SIZE, (int) (currentFont.getSize() * 1.8)); + Font myFont = Font.getFont(attributes); + g2.setFont(myFont); + + // temporarily save default component color to draw text background in specific color + Color tempOne = g2.getColor(); + + // draw background in specific color + g2.setColor(Constants.COLOR_SCOREBOARD); + + // centered mid-point x-val + int midX = Constants.SCREEN_WIDTH*5/6; + int offset = Constants.SCOREBOARD_OFFSET; + int textOffsetY = 20; // somehow handcrafted offset + + // calculate rendered string width + int scoreWidth = g2.getFontMetrics().stringWidth(Constants.SCOREBOARD_SCORE + view.getGame().getLevel().getScore()); + int lifeWidth = g2.getFontMetrics().stringWidth(Constants.SCOREBOARD_LIVES + view.getGame().getLevel().getLives()); + int maxWidth = Math.max(scoreWidth, lifeWidth); // max width + int fontSize = g2.getFontMetrics().getHeight(); // font size + + // fillRoundRect(x, y, width, height, arcWidth, arcHeight) + g2.fillRoundRect((midX)-offset-maxWidth/2, Constants.SCREEN_HEIGHT-2*offset-textOffsetY-2*fontSize, maxWidth+2*offset, 2*(fontSize+offset), 5, 5); + + // reset to default color + g2.setColor(tempOne); + + // temporarily save default component color to draw text in specific color + Color temp = g2.getColor(); + g2.setColor(Constants.COLOR_TEXT); + + // draw score and lives + drawScore(g2, midX, Constants.SCREEN_HEIGHT-fontSize); + drawLives(g2, midX, Constants.SCREEN_HEIGHT-2*(fontSize)-textOffsetY); + + // reset color and font + g2.setColor(temp); + g2.setFont(currentFont); + } + + /** + * Draws the score + * + * @param g2 The graphics object + * @param x middle position on x-axis + * @param y y-axis + */ + private void drawScore(Graphics2D g2, int x, int y) { + int score = view.getGame().getLevel().getScore(); + + String str = Constants.SCOREBOARD_SCORE+score; + + x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis + + // draw string + g2.drawString(str, x, y); + } + + /** + * Draws the life counter + * + * @param g2 The graphics object + * @param x middle position on x-axis + * @param y y-axis + */ + private void drawLives(Graphics2D g2, int x, int y) { + int lives = view.getGame().getLevel().getLives(); + + String str = Constants.SCOREBOARD_LIVES+lives; + + x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis + + // draw string + g2.drawString(str, x, y); + } + }