-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock2048.java
More file actions
273 lines (240 loc) · 7.79 KB
/
Block2048.java
File metadata and controls
273 lines (240 loc) · 7.79 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package game2048;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Block2048
{
int blockValue;
int xCoord;
int yCoord;
BufferedImage img;
BufferedImage num;
boolean lockedIn;
boolean newCombination = false;
int blockWidth;
int blockHeight;
int spaceBetween;
int edgeSpace;
ImageIcon pic = new ImageIcon();
JLabel blockJLabel;
JFrame blockJFrame;
int borderWidth;
public Block2048(JFrame frame, int passedValue, boolean locked)
{
this.blockValue = passedValue;
lockedIn = locked;
//Setting an image to the block depending on its value
blockJFrame = frame;
blockJLabel = new JLabel();
blockJLabel.setBounds (10, 10, 10, 10); // arbitrary, will change later
blockJFrame.getContentPane().add(blockJLabel);
blockJLabel.setVisible(false);
blockJLabel.setVisible(true);
newCombination = false;
borderWidth = 5;
try
{
img = ImageIO.read(new File("BlockImage.png"));
blockWidth = 66;
blockHeight = 66;
spaceBetween = 9;
edgeSpace = 5;
blockJLabel = new JLabel();
blockJLabel.setBounds (10, 10, blockWidth, blockHeight); // arbitrary, will change later
Controller2048.gameFrame2048.getContentPane().add(blockJLabel);
blockJLabel.setVisible(false);
blockJLabel.setVisible(true);
switch(blockValue)
{
case 0:
num = null;
break;
case 2:
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockHeight, edgeSpace + 3*spaceBetween + 3*blockWidth, blockWidth, blockHeight);
break;
case 4:
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth, edgeSpace + 2*spaceBetween + 2*blockWidth, blockWidth, blockHeight);
break;
case 8:
num = img.getSubimage(edgeSpace + spaceBetween + blockWidth, edgeSpace + 3*spaceBetween + 3*blockHeight, blockWidth, blockHeight);
break;
case 16:
num = img.getSubimage(edgeSpace, edgeSpace + 3*spaceBetween + 3*blockHeight, blockWidth, blockHeight);
break;
case 32:
num = img.getSubimage(edgeSpace , edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 64:
num = img.getSubimage(edgeSpace + spaceBetween + blockWidth, edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 128:
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth, edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 256:
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockWidth, edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 512:
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockWidth, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 1024:
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 2048:
num = img.getSubimage(edgeSpace + spaceBetween + blockWidth, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 4096:
num = img.getSubimage(edgeSpace, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 8192:
num = img.getSubimage(edgeSpace, edgeSpace, blockWidth, blockHeight);
break;
}
if(num != null)
{
pic = new ImageIcon (num);
}
} catch (IOException e)
{
System.out.println("Image reading error!");
e.printStackTrace();
}
}
public int getValue()
{
return blockValue;
}
public void setBlockValue(int passedValue)
{
this.blockValue = passedValue;
switch(blockValue)
{
case 0:
num = null;
//if a block is 0 it will not fall.
setLockedIn(true);
break;
case 2:
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockHeight-1, edgeSpace + 3*spaceBetween + 3*blockWidth, blockWidth, blockHeight);
break;
case 4:
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth, edgeSpace + 3*spaceBetween + 3*blockWidth, blockWidth, blockHeight);
break;
case 8:
num = img.getSubimage(edgeSpace + spaceBetween + blockWidth, edgeSpace + 3*spaceBetween + 3*blockHeight, blockWidth, blockHeight);
break;
case 16:
num = img.getSubimage(edgeSpace, edgeSpace + 3*spaceBetween + 3*blockHeight, blockWidth, blockHeight);
break;
case 32:
num = img.getSubimage(edgeSpace , edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 64:
num = img.getSubimage(edgeSpace + spaceBetween + blockWidth, edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 128:
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth, edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 256:
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockWidth, edgeSpace + 2*spaceBetween + 2*blockHeight, blockWidth, blockHeight);
break;
case 512:
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockWidth, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 1024:
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 2048:
num = img.getSubimage(edgeSpace + spaceBetween + blockWidth, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 4096:
num = img.getSubimage(edgeSpace, edgeSpace + spaceBetween + blockHeight, blockWidth, blockHeight);
break;
case 8192:
num = img.getSubimage(edgeSpace, edgeSpace, blockWidth, blockHeight);
break;
}
if(num != null)
{
pic = new ImageIcon (num);
}
}
public int getX()
{
return xCoord;
}
public int getY()
{
return yCoord;
}
public void setX(int x)
{
xCoord = x;
}
public void setY(int y)
{
yCoord = y;
}
public int getArrayX()
{
return xCoord/67;
}
public int getArrayY()
{
return yCoord/67;
}
public boolean getLockedIn()
{
return lockedIn;
}
public void setLockedIn(boolean isLocked)
{
lockedIn = isLocked;
}
public boolean isNewCombination(){
return newCombination;
}
public void setNewCombination(boolean passedNewCombination) {
//System.out.println("SET BLOCK " + this.yCoord/67 +", "+ this.xCoord/67 + " NEW COMBO TO: " + passedNewCombination);
newCombination = passedNewCombination;
}
public void drawBlock()
{
if(blockValue != 0)
{
blockJLabel.setIcon(pic);
blockJLabel.setBounds(xCoord,yCoord,pic.getIconWidth(),pic.getIconHeight());
blockJLabel.setVisible(true);
}
else
{
blockJLabel.setVisible(false);
}
}
public void addBorder()
{
javax.swing.border.Border border = BorderFactory.createLineBorder(Color.YELLOW, borderWidth);
blockJLabel.setBorder(border);
if(blockValue == 2)
{
num = img.getSubimage(edgeSpace + 3*spaceBetween + 3*blockHeight + 4, edgeSpace + 3*spaceBetween + 3*blockWidth , blockWidth , blockHeight );
}
else if(blockValue == 4)
{
num = img.getSubimage(edgeSpace + 2*spaceBetween + 2*blockWidth + 4, edgeSpace + 3*spaceBetween + 3*blockWidth , blockWidth , blockHeight );
}
pic = new ImageIcon (num);
}
public void removeBorder()
{
blockJLabel.setBorder(null);
setBlockValue(blockValue);
drawBlock();
}
}