-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicsBoard.java
More file actions
233 lines (195 loc) · 7.39 KB
/
Copy pathGraphicsBoard.java
File metadata and controls
233 lines (195 loc) · 7.39 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.*;
public class GraphicsBoard extends JFrame{
private JLabel screen;
private Block[][] board;
private BufferedImage finalImage;
private final Block emptyBlock = new Block(0);
private JLabel currentScore;
private JLabel highScore;
public GraphicsBoard(BoardGUI g){
super("2048");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
board = new Block[4][4];
JPanel p = new JPanel(new BorderLayout(1,1));
screen = new JLabel();
p.add(screen,BorderLayout.PAGE_START);
//create current score and high score labels
currentScore = new JLabel();
final BufferedImage img = new BufferedImage(160,40,BufferedImage.TYPE_INT_RGB);
Graphics2D graph= img.createGraphics();
Rectangle rect1 = new Rectangle(160,40);
graph.draw(rect1);
graph.setColor(Color.CYAN);
graph.fill(rect1);
Rectangle rect2 = new Rectangle(10,5,140,30);
graph.draw(rect2);
graph.setColor(Color.WHITE);
graph.fill(rect2);
graph.dispose();
currentScore.setIcon(new ImageIcon(img));
currentScore.setHorizontalTextPosition(JLabel.CENTER);
currentScore.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
updateCurrentScore(0);
highScore = new JLabel();
final BufferedImage img2 = new BufferedImage(160,40,BufferedImage.TYPE_INT_RGB);
graph= img2.createGraphics();
graph.draw(rect1);
graph.setColor(Color.CYAN);
graph.fill(rect1);
graph.draw(rect2);
graph.setColor(Color.WHITE);
graph.fill(rect2);
graph.dispose();
highScore.setIcon(new ImageIcon(img2));
highScore.setHorizontalTextPosition(JLabel.CENTER);
highScore.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
updateHighScore(0);
p.add(currentScore, BorderLayout.LINE_START);
p.add(highScore, BorderLayout.LINE_END);
add(p);
}
/**
* update fields of graphics board:
**/
//update display image based on current board field in GraphicsBoard
public void updateFinalImage(){
final BufferedImage img = new BufferedImage(320,320,BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
for (int x = 0; x < 4; x++){
for (int y = 0; y < 4; y++){
g.drawImage(board[x][y].getImage(),x*80,y*80,null);
}
}
g.dispose();
finalImage = img;
display();
}
//update elements in board at coordinates x and y with specific number
public void updateBoard(int x, int y, int num){
board[x][y] = new Block(num);
}
public void display(){
screen.setIcon(new ImageIcon(finalImage));
}
/**
* Update this board with most recent movement coordinates
*/
public void downUpdate(int[][] coordinates){
for (int i = 0; i < 4; i++){
for (int j = 3; j >= 0; j--){
if (coordinates[i][j] == 1){
updateBoard(i,j+1,board[i][j].getValue());
updateBoard(i,j,0);
} else if (coordinates[i][j] > 1){
updateBoard(i,j+1,board[i][j].getValue()*2);
updateBoard(i,j,0);
}
}
}
}
public void upUpdate(int[][] coordinates){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if (coordinates[i][j] == 1){
updateBoard(i,j-1,board[i][j].getValue());
updateBoard(i,j,0);
} else if (coordinates[i][j] > 1){
updateBoard(i,j-1,board[i][j].getValue()*2);
updateBoard(i,j,0);
}
}
}
}
public void leftUpdate(int[][] coordinates){
for (int j = 3; j >= 0; j--){
for (int i = 0; i < 4; i++){
if (coordinates[i][j] == 1){
updateBoard(i-1,j,board[i][j].getValue());
updateBoard(i,j,0);
} else if (coordinates[i][j] > 1){
updateBoard(i-1,j,board[i][j].getValue()*2);
updateBoard(i,j,0);
}
}
}
}
public void rightUpdate(int[][] coordinates){
for (int j = 3; j >= 0; j--){
for (int i = 3; i >= 0; i--){
if (coordinates[i][j] == 1){
updateBoard(i+1,j,board[i][j].getValue());
updateBoard(i,j,0);
} else if (coordinates[i][j] > 1){
updateBoard(i+1,j,board[i][j].getValue()*2);
updateBoard(i,j,0);
}
}
}
}
/**
* Update screen with most recent image based on coordinates
* and movement updates
*/
public void drawMove(int[][] coordinates, int xLength, int yLength){
final BufferedImage img = new BufferedImage(320,320,BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
//layer 1: nonmoving blocks
for (int x = 0; x < 4; x++){
for (int y = 0; y < 4; y++){
if (coordinates[x][y] == 0){
g.drawImage(board[x][y].getImage(),80*x,80*y,null);
} else{
g.drawImage(emptyBlock.getImage(),80*x, 80*y, null);
}
}
}
//layer 2: moving blocks
for (int x = 0; x < 4; x++){
for (int y = 0; y < 4; y++){
if (coordinates[x][y] != 0){
g.drawImage(board[x][y].getImage(),80*x+xLength,80*y+yLength,null);
}
}
}
g.dispose();
finalImage = img;
display();
}
public void drawCombines(int[][] coordinates, int xLength, int yLength, int dilation){
Graphics2D g = finalImage.createGraphics();
for (int x = 0; x < 4; x++){
for (int y = 0; y < 4; y++){
if (coordinates[x][y] > 1){
int newNumber = coordinates[x][y];
g.drawImage((new Block(newNumber,dilation)).getImage(),80*x+xLength,80*y+yLength,null);
}
}
}
g.dispose();
display();
}
public void drawNewValue(int x, int y, int val, int dilation){
Graphics2D g = finalImage.createGraphics();
g.drawImage((new Block(val,dilation)).getImage(),80*x,80*y,null);
g.dispose();
display();
}
public void drawPlayAgain(){
Graphics2D g = finalImage.createGraphics();
Font f = new Font("TIMES NEW ROMAN", Font.BOLD, 30);
g.setFont(f);
g.setColor(Color.YELLOW);
g.drawString("PRESS ENTER" ,40,60);
g.drawString("TO PLAY AGAIN",40,90);
g.dispose();
display();
}
public void updateCurrentScore(int score){
currentScore.setText("SCORE: "+score);
}
public void updateHighScore(int score){
highScore.setText("HIGH SCORE: "+score);
}
}