-
Notifications
You must be signed in to change notification settings - Fork 0
/
Location.java
304 lines (150 loc) · 4.81 KB
/
Location.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//Amy Wang
//Final Project Part 1 - Location.java
//5/8/15
//Essentially quite similar to GridWorld's Location class--a simple square region with a
//coordinate x and y value representing its position on the overall Tetris board. Can be filled or
//emptied with color.
import java.util.*;
import java.awt.*;
public class Location
{
/* The side length of the Location, when drawn on the JPanel
*/
private int size;
/* The color of the Location--the color with which Location is filled in
*/
private Color color;
/* The x coordinate of the Location (in the context of the coordinate-system of the
* TetrisBoard)
*/
private int xcoord;
/* The y coordinate of the Location
*/
private int ycoord;
/* The upper left point of the Location square
*/
private Point top;
/* Tells whether or not the Location is being occupied by a TetrisPiece
*/
private boolean full;
/* The Graphics object upon which the Location will be drawn
*/
private Graphics g;
/* Constructor method; uses the passed in parameter values to create a new Location, which is
* essentially a coordinate square that can be filled or emptied with a Color.
* Full is initially set to false.
* @param size - the length of one side of the Location object
* @param x - the x coordinate of the Location (the TetrisBoard is a 2D array of
* Locations, and thus a type of coordinate system)
* @param y - the y coordinate of the Location
* @param c - the Color with which the Location will be filled, if full
*/
public Location(int size, int x, int y, Color c)
{
this.size = size;
xcoord = x; //corresponds to column number
ycoord = y; //corresponds to row number
top = new Point(x*size, y*size);
color = c;
this.full = false;
}
/* Returns the "top," or upper-left corner of the Location's coordinate
* square.
* @return top - the upper-left point of the Location, when the Location is
* drawn
*/
public Point getTop()
{
return top;
}
/* Returns the x coordinate of the Location
* @return xcoord - the x coordinate of the Location (generally
* in relation to the TetrisBoard)
*/
public int getX()
{
return xcoord;
}
/* Returns the y coordinate of the Location
* @return ycoord - the y coordinate of the Location (generally
* in relation to the TetrisBoard)
*/
public int getY()
{
return ycoord;
}
/* Returns the size of the side length of the Location
* @return size - the size of one side of the Location, when
* drawn on the JPanel
*/
public int getSize()
{
return size;
}
/* Checks to see if the Location is full (Locations are full if occupied
* by either a moving or resting Tile)
* @return full - a boolean representing whether or not the Location is full
*/
public boolean isFull()
{
return full;
}
/* "Fills" the Location by setting full to true
*/
public void fill()
{
full = true;
}
/* "Empties" the Location by setting full to false
*/
public void empty()
{
full = false;
}
public Color getColor()
{
return color;
}
/* Sets the Color of the Location to the passed-in parameter
* -- generally utilized when drawing in Locations on the TetrisBoard
* @param c - the Color the Location will later be drawn in
*/
public void setColor(Color c)
{
color = c;
}
/* Draws the Location on the JPanel by filling in a square of side length size
* starting from the Point "top"
* @param graphics - the Graphics object that will help draw the Location
*/
public void drawSelf(Graphics graphics)
{
if(graphics!=null)
{
if(full)
{
graphics.setColor(color);
}
else
{
graphics.setColor(Color.BLACK);
}
//System.out.println("" + (int)top.getX() + (int)top.getY());
graphics.fillRect((int)top.getX(), (int)top.getY(), size, size);
}
}
/* Equals method - checks if the two Locations share the same top left corner (or "starting point")
* as well the same side length
* @param other - the other Location, to be compared to this one
* @return - true if the two Locations are located at the same place as one another, and
* also have the same side length
*/
public boolean equals(Location other)
{
if(other.getTop()==this.getTop() && other.getSize()==this.getSize())
{
return true;
}
return false;
}
}