-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.java
73 lines (59 loc) · 2.25 KB
/
View.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package project2048;
import javax.swing.*;
import java.awt.*;
/**
* @author javarush
*/
public class View extends JPanel {
private static final Color BG_COLOR = new Color(0xbbada0);
private static final String FONT_NAME = "Arial";
private static final int TILE_SIZE = 96;
private static final int TILE_MARGIN = 12;
private Controller controller;
boolean isGameWon = false;
boolean isGameLost = false;
public View(Controller controller) {
setFocusable(true);
this.controller = controller;
addKeyListener(controller);
}
@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);
}
private static int offsetCoors(int arg) {
return arg * (TILE_MARGIN + TILE_SIZE) + TILE_MARGIN;
}
}