Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed the font-size [issue 7] #10

Merged
merged 8 commits into from
Dec 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class Renderer2048 {

private static final int TILE_SIDE = 200;
private static final int PADDING = 20;
private static final int DEFAULT_FONT_SIZE = 120;
private static final int SCALE = 8;
private static Map<Integer, Color> tileColorMap = new HashMap<>();

private Game2048 game;
Expand Down Expand Up @@ -54,6 +56,10 @@ public void setGame(Game2048 game) {
this.game = game;
}

private static int getFontSizeForTileValue(int tileValue) {
return DEFAULT_FONT_SIZE - String.valueOf(tileValue).length() * SCALE;
}

public byte[] getData() {
Graphics2D graphics = image.createGraphics();

Expand All @@ -74,7 +80,7 @@ public byte[] getData() {
if (tileValue == 0) continue;

graphics.setColor(tileValue < 8 ? new Color(130, 115, 100) : Color.WHITE);
graphics.setFont(new Font("Arial", Font.BOLD, tileValue > 1000 ? 80 : 100));
graphics.setFont(new Font("Arial", Font.BOLD, getFontSizeForTileValue(tileValue)));
FontMetrics tileMetrics = graphics.getFontMetrics();
String tileText = String.valueOf(tileValue);
graphics.drawString(tileText,
Expand All @@ -96,7 +102,7 @@ public byte[] getData() {
};

graphics.setColor(Color.BLACK);
graphics.setFont(new Font("Arial", Font.BOLD, 120));
graphics.setFont(new Font("Arial", Font.BOLD, DEFAULT_FONT_SIZE));
FontMetrics metrics = graphics.getFontMetrics();
graphics.drawString(text,
(image.getWidth() - metrics.stringWidth(text)) / 2,
Expand Down
Loading