1
0
Fork 0

BreakOut 5.2 - methods are now written according to specifications to pass JUnit tests

master
rxbn_ 3 years ago
parent 90afe2d72e
commit 59fdc12e4f
  1. 7
      src/break_out/Constants.java
  2. 43
      src/break_out/view/Field.java

@ -1,5 +1,7 @@
package break_out;
import break_out.model.Position;
import java.awt.Color;
/**
@ -119,4 +121,9 @@ public class Constants {
*/
public static final String SCOREBOARD_LIVES = "Lives: ";
/**
* The position for the scoreboards midpoint (bottom center)
*/
public static final Position SCOREBOARD_MIDPOINT = new Position(SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
}

@ -271,16 +271,17 @@ public class Field extends JPanel {
Font myFont = Font.getFont(attributes);
g2.setFont(myFont);
// temporarily save default component color to draw text background in specific color
Color tempOne = g2.getColor();
// temporarily save default component color to draw text and background in specific color
Color temp = g2.getColor();
// draw background in specific color
g2.setColor(Constants.COLOR_SCOREBOARD);
// centered mid-point x-val
int midX = Constants.SCREEN_WIDTH*5/6;
// bottom centered mid-point
int midX = (int) Constants.SCOREBOARD_MIDPOINT.getX();
int midY = (int) Constants.SCOREBOARD_MIDPOINT.getY();
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());
@ -289,18 +290,14 @@ public class Field extends JPanel {
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);
g2.fillRoundRect((midX)-offset-maxWidth/2, midY-2*offset-3*fontSize, maxWidth+2*offset, 2*(fontSize+offset), 5, 5);
// temporarily save default component color to draw text in specific color
Color temp = g2.getColor();
// draw text in specific color (sharing g2 object and therefore also color)
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);
drawScore(g2);
drawLives(g2);
// reset color and font
g2.setColor(temp);
@ -311,12 +308,16 @@ public class Field extends JPanel {
* 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) {
private void drawScore(Graphics2D g2) {
int score = view.getGame().getLevel().getScore();
int fontSize = g2.getFontMetrics().getHeight(); // font size
// bottom centered mid-point
int x = (int) Constants.SCOREBOARD_MIDPOINT.getX();
int y = (int) Constants.SCOREBOARD_MIDPOINT.getY()-(int)((3.0/2.0)*fontSize);
String str = Constants.SCOREBOARD_SCORE+score;
x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis
@ -329,12 +330,16 @@ public class Field extends JPanel {
* 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) {
private void drawLives(Graphics2D g2) {
int lives = view.getGame().getLevel().getLives();
int fontSize = g2.getFontMetrics().getHeight(); // font size
// bottom centered mid-point
int x = (int) Constants.SCOREBOARD_MIDPOINT.getX();
int y = (int) Constants.SCOREBOARD_MIDPOINT.getY()-3*fontSize;
String str = Constants.SCOREBOARD_LIVES+lives;
x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis

Loading…
Cancel
Save