This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTile.java
More file actions
298 lines (261 loc) · 7.75 KB
/
Tile.java
File metadata and controls
298 lines (261 loc) · 7.75 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Random;
import java.util.Set;
import javafx.animation.FillTransition;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.util.Duration;
/**
*
* @author frank
*/
public class Tile extends Observable
{
/**
* Possible Tile Types for each Tile
*/
public static enum Type
{
ROOT,
TARGET,
WALL,
EMPTY,
PATH,
HIGHLIGHT,
VISITED,
VISITED_LIGHT,
VISITED_MEDIUM,
VISITED_DENSE,
VISITED_MAX
}
private final Map<Type, Color> typeMap;
private final Map<Integer, Color> weightMap;
private final Map<Integer, Color> visitedMap;
private final int[] WEIGHTS = { this.getDefaultWeight(), 3, 6, 9, 12};
// JavaFX Node
private final StackPane pane;
// Coordinates
private final int x;
private final int y;
// Content
private final Rectangle rectangle;
// Attributes
private final int defaultWeight = 1;
private int weight;
private Type type;
private final double tileGap = 0;
private final int size;
public Tile(int x, int y, int size)
{
pane = new StackPane();
// Type color Mapping
typeMap = new HashMap<>();
typeMap.put(Type.ROOT, Color.YELLOW);
typeMap.put(Type.TARGET, Color.PURPLE);
typeMap.put(Type.EMPTY, Color.WHITE);
typeMap.put(Type.WALL, Color.BLACK);
typeMap.put(Type.PATH, Color.DEEPPINK);
typeMap.put(Type.HIGHLIGHT, Color.RED);
typeMap.put(Type.VISITED, Color.LIGHTGREEN);
// Visited Color based on weight
visitedMap = new HashMap<>();
visitedMap.put(this.WEIGHTS[0], Color.PALEGREEN);
visitedMap.put(this.WEIGHTS[1], Color.LIGHTGREEN);
visitedMap.put(this.WEIGHTS[2], Color.SPRINGGREEN);
visitedMap.put(this.WEIGHTS[3], Color.GREENYELLOW);
visitedMap.put(this.WEIGHTS[4], Color.GREEN);
// Empty Weight color
weightMap = new HashMap<>();
weightMap.put(this.WEIGHTS[0], Color.WHITE);
weightMap.put(this.WEIGHTS[1], Color.LIGHTCYAN);
weightMap.put(this.WEIGHTS[2], Color.AQUA);
weightMap.put(this.WEIGHTS[3], Color.DEEPSKYBLUE);
weightMap.put(this.WEIGHTS[4], Color.CORNFLOWERBLUE);
// Coordinates
this.x = x;
this.y = y;
// Attributes
this.weight = defaultWeight;
this.type = Type.EMPTY;
this.size = size;
// Tile content
this.rectangle = new Rectangle(size - tileGap, size - tileGap);
this.rectangle.setFill(Color.WHITE);
this.setTileStroke(true);
// build this StackPane
pane.getChildren().add(rectangle);
pane.setTranslateX(x * size);
pane.setTranslateY(y * size);
updateTooltip(null);
setEvents();
}
/**
* Returns this Tile's pane
* @return StackPane
*/
public StackPane getStackPane()
{
return this.pane;
}
/**
* Toggles coordinates to appear in this tiles' pane
* @param toAdd true if it's to add coords, false if it's to remove
S */
public void toggleCoords(boolean toAdd)
{
if(pane.getChildren().size() > 1) pane.getChildren().remove(1);
if(toAdd)
{
Text coords = new Text(String.format("%d,%d", this.x, this.y));
// Scale: For a tile of size 20, font size is 6
coords.setStyle(String.format("-fx-font: %d arial;", (size * 6) / 20));
Color color = (this.getType() == Tile.Type.WALL) ? Color.WHITE : Color.BLACK;
coords.setFill(color);
pane.getChildren().add(coords);
}
}
/**
* toggles tile stroke based on parameter true or false
* @param setStroke boolean, true to set a visible stroke, false to remove
*/
public void setTileStroke(boolean setStroke)
{
this.rectangle.setStroke((setStroke) ? Color.LIGHTGRAY : null);
}
/**
* Add text to this tile
* @param text
*/
public void addText(String text)
{
this.toggleCoords(false);
Text txt = new Text(text);
// Scale: For a tile of size 20, font size is 6
txt.setStyle(String.format("-fx-font: %d arial;", (size * 6) / 20));
pane.getChildren().add(txt);
}
/**
* Sets both Type and Weight attributes for this tile.
* @param type of this tile
* @param weight of this tile
*/
public void setAttributes(Type type, int weight)
{
Color color;
// Pick color based on type
switch(type)
{
case VISITED:
color = this.visitedMap.get(this.getWeight());
break;
case EMPTY:
color = this.weightMap.get(weight);
break;
default:
color = this.typeMap.get(type);
break;
}
this.rectangle.setFill(color);
this.type = type;
this.weight = weight;
updateTooltip(null);
}
/**
* Returns this tiles weight
* @return
*/
public int getWeight()
{
return this.weight;
}
/**
* Returns x coordinate of this tile
* @return
*/
public int getX()
{
return this.x;
}
/**
* Returns y coordinate of this tile
* @return
*/
public int getY()
{
return this.y;
}
/**
* Returns given Tile to its default state (EMPTY, DEFAULTWEIGHT)
* @return
*/
public void clearTile()
{
this.setAttributes(Type.EMPTY, defaultWeight);
}
/**
* Returns default weight for an empty tile
* @return
*/
public int getDefaultWeight()
{
return this.defaultWeight;
}
public Type getType()
{
return this.type;
}
/**
* Randomizes this tile's weight
*/
public void randomizeWeight()
{
Set<Integer> keys = weightMap.keySet();
Random random = new Random();
this.setAttributes(Type.EMPTY,
(int)keys.toArray()[random.nextInt(keys.size())]
);
}
public boolean isWall()
{
return (this.type == Type.WALL);
}
/**
* Updates this tile's Tooltip
* @param text
*/
private void updateTooltip(String text)
{
text = (text == null) ? this.toString() : text;
Tooltip.install(pane, new Tooltip(text));
}
/**
* Defines events for this Tile
*/
private void setEvents()
{
pane.addEventFilter(MouseEvent.MOUSE_PRESSED, (MouseEvent me) ->
{
// Notifies the @Grid
setChanged();
notifyObservers();
});
}
@Override
public String toString()
{
return String.format("%s - (%d,%d) W:%d", this.type, this.x, this.y, this.weight);
}
}