@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(BG_COLOR);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
drawTile(g, controller.getGameTiles()[y][x], x, y);
}
}
g.drawString("Score: " + controller.getScore(), 140, 465);
if (isGameWon) {
JOptionPane.showMessageDialog(this, "You've won!");
} else if(isGameLost) {
JOptionPane.showMessageDialog(this, "You've lost :(");
}
}
private void drawTile(Graphics g2, Tile tile, int x, int y) {
Graphics2D g = ((Graphics2D) g2);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int value = tile.value;
int xOffset = offsetCoors(x);
int yOffset = offsetCoors(y);
g.setColor(tile.getTileColor());
g.fillRoundRect(xOffset, yOffset, TILE_SIZE, TILE_SIZE , 8, 8);
g.setColor(tile.getFontColor());
final int size = value < 100 ? 36 : value < 1000 ? 32 : 24;
final Font font = new Font(FONT_NAME, Font.BOLD, size);
g.setFont(font);
String s = String.valueOf(value);
final FontMetrics fm = getFontMetrics(font);
final int w = fm.stringWidth(s);
final int h = -(int) fm.getLineMetrics(s, g).getBaselineOffsets()[2];
if (value != 0)
g.drawString(s, xOffset + (TILE_SIZE - w) / 2, yOffset + TILE_SIZE - (TILE_SIZE - h) / 2 - 2);
}