Skip to content

Commit

Permalink
Better display of score
Browse files Browse the repository at this point in the history
  • Loading branch information
spypunk committed Sep 28, 2016
1 parent bffde0d commit 9b1df2f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main/java/spypunk/snake/ui/font/FontType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public enum FontType {

DEFAULT(30F, Font.NEUTRONIUM),
SCORE(40F, Font.NEUTRONIUM),
URL(10F, Font.RUSSO_ONE),
FROZEN(42F, Font.NEUTRONIUM);

Expand Down
13 changes: 6 additions & 7 deletions src/main/java/spypunk/snake/ui/view/SnakeInstanceScoreView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
Expand All @@ -28,19 +27,21 @@

public class SnakeInstanceScoreView extends AbstractSnakeInstanceView {

private static final String EMPTY_STRING = "".intern();

private static final long serialVersionUID = 3093168306699870331L;

private final Rectangle scoreRectangle = new Rectangle(0, 0, 10 * CELL_SIZE, CELL_SIZE);

private final Font defaultFont;
private final Font scoreFont;

private final Snake snake;

public SnakeInstanceScoreView(final FontCache fontCache,
final Snake snake) {
this.snake = snake;

defaultFont = fontCache.getFont(FontType.DEFAULT);
scoreFont = fontCache.getFont(FontType.SCORE);

image = new BufferedImage(scoreRectangle.width, scoreRectangle.height,
BufferedImage.TYPE_INT_ARGB);
Expand All @@ -59,10 +60,8 @@ public void update() {
private void renderScore(final Graphics2D graphics) {
final SnakeInstance snakeInstance = snake.getSnakeInstance();

final String score = snakeInstance == null ? "0" : String.valueOf(snakeInstance.getScore());

final Rectangle2D textBounds = SwingUtils.getTextBounds(graphics, score, defaultFont);
final String score = snakeInstance == null ? EMPTY_STRING : String.valueOf(snakeInstance.getScore());

SwingUtils.renderText(graphics, score, textBounds.getBounds(), defaultFont, DEFAULT_FONT_COLOR);
SwingUtils.renderCenteredText(graphics, score, scoreRectangle, scoreFont, DEFAULT_FONT_COLOR);
}
}
19 changes: 12 additions & 7 deletions src/main/java/spypunk/snake/ui/view/SnakeViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,18 @@ public SnakeViewImpl(final SnakeController snakeController,
bottomPanel.setBorder(BorderFactory.createEmptyBorder(CELL_SIZE, 3, 3, 3));
bottomPanel.setBackground(Color.BLACK);

final JPanel scorePanel = new JPanel(new BorderLayout());
final JPanel topPanel = new JPanel(new BorderLayout());

scorePanel.add(snakeInstanceScoreView, BorderLayout.CENTER);
scorePanel.add(snakeInstanceNormalStatisticView, BorderLayout.WEST);
scorePanel.add(snakeInstanceBonusStatisticView, BorderLayout.EAST);
scorePanel.setBackground(Color.BLACK);
scorePanel.setBorder(BorderFactory.createEmptyBorder(CELL_SIZE / 2, CELL_SIZE, CELL_SIZE / 2, CELL_SIZE));
final JPanel statisticsPanel = new JPanel(new BorderLayout(0, 3));

statisticsPanel.add(snakeInstanceNormalStatisticView, BorderLayout.NORTH);
statisticsPanel.add(snakeInstanceBonusStatisticView, BorderLayout.SOUTH);
statisticsPanel.setBackground(Color.BLACK);

topPanel.add(statisticsPanel, BorderLayout.WEST);
topPanel.add(snakeInstanceScoreView, BorderLayout.CENTER);
topPanel.setBackground(Color.BLACK);
topPanel.setBorder(BorderFactory.createEmptyBorder(CELL_SIZE / 2, CELL_SIZE, CELL_SIZE / 2, CELL_SIZE));

bottomPanel.add(muteLabel, BorderLayout.WEST);
bottomPanel.add(urlLabel, BorderLayout.EAST);
Expand All @@ -167,7 +172,7 @@ public SnakeViewImpl(final SnakeController snakeController,
frame.addKeyListener(new SnakeViewKeyAdapter(snakeController));
frame.setIconImage(imageCache.getIcon(Icon.ICON));

frame.add(scorePanel, BorderLayout.NORTH);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.pack();
Expand Down

0 comments on commit 9b1df2f

Please sign in to comment.