1
0
Fork 0

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

This commit is contained in:
rxbn_ 2020-02-01 09:50:56 +01:00
parent 90afe2d72e
commit 59fdc12e4f
2 changed files with 31 additions and 19 deletions

View File

@ -1,5 +1,7 @@
package break_out; package break_out;
import break_out.model.Position;
import java.awt.Color; import java.awt.Color;
/** /**
@ -119,4 +121,9 @@ public class Constants {
*/ */
public static final String SCOREBOARD_LIVES = "Lives: "; 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);
} }

View File

@ -271,16 +271,17 @@ public class Field extends JPanel {
Font myFont = Font.getFont(attributes); Font myFont = Font.getFont(attributes);
g2.setFont(myFont); g2.setFont(myFont);
// temporarily save default component color to draw text background in specific color // temporarily save default component color to draw text and background in specific color
Color tempOne = g2.getColor(); Color temp = g2.getColor();
// draw background in specific color // draw background in specific color
g2.setColor(Constants.COLOR_SCOREBOARD); g2.setColor(Constants.COLOR_SCOREBOARD);
// centered mid-point x-val // bottom centered mid-point
int midX = Constants.SCREEN_WIDTH*5/6; int midX = (int) Constants.SCOREBOARD_MIDPOINT.getX();
int midY = (int) Constants.SCOREBOARD_MIDPOINT.getY();
int offset = Constants.SCOREBOARD_OFFSET; int offset = Constants.SCOREBOARD_OFFSET;
int textOffsetY = 20; // somehow handcrafted offset
// calculate rendered string width // calculate rendered string width
int scoreWidth = g2.getFontMetrics().stringWidth(Constants.SCOREBOARD_SCORE + view.getGame().getLevel().getScore()); 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 int fontSize = g2.getFontMetrics().getHeight(); // font size
// fillRoundRect(x, y, width, height, arcWidth, arcHeight) // 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); g2.fillRoundRect((midX)-offset-maxWidth/2, midY-2*offset-3*fontSize, maxWidth+2*offset, 2*(fontSize+offset), 5, 5);
// reset to default color // draw text in specific color (sharing g2 object and therefore also color)
g2.setColor(tempOne);
// temporarily save default component color to draw text in specific color
Color temp = g2.getColor();
g2.setColor(Constants.COLOR_TEXT); g2.setColor(Constants.COLOR_TEXT);
// draw score and lives // draw score and lives
drawScore(g2, midX, Constants.SCREEN_HEIGHT-fontSize); drawScore(g2);
drawLives(g2, midX, Constants.SCREEN_HEIGHT-2*(fontSize)-textOffsetY); drawLives(g2);
// reset color and font // reset color and font
g2.setColor(temp); g2.setColor(temp);
@ -311,12 +308,16 @@ public class Field extends JPanel {
* Draws the score * Draws the score
* *
* @param g2 The graphics object * @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 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; String str = Constants.SCOREBOARD_SCORE+score;
x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis 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 * Draws the life counter
* *
* @param g2 The graphics object * @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 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; String str = Constants.SCOREBOARD_LIVES+lives;
x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis x -= g2.getFontMetrics().stringWidth(str)/2; // because x is middle position on x axis