1
0
Fork 0

BreakOut 5.2 - Scoreboard

master
rxbn_ 3 years ago
parent 21a7b951d6
commit 90afe2d72e
  1. 25
      src/break_out/Constants.java
  2. 96
      src/break_out/view/Field.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: ";
}

@ -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: <a href="https://stackoverflow.com/questions/18249592/"> Stackoverflow Question - 18249592 </a>
Map<TextAttribute, Object> 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);
}
}

Loading…
Cancel
Save