-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc_FieldDisplay.java
More file actions
76 lines (62 loc) · 2.03 KB
/
src_FieldDisplay.java
File metadata and controls
76 lines (62 loc) · 2.03 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
import java.util.LinkedHashMap;
import java.util.Map;
import processing.core.*;
public class FieldDisplay {
private static final int EMPTY_COLOR = 0xFFFFFFFF;
private static final int UNKNOWN_COLOR = 0x66666666;
private PApplet p;
private Field f;
private int x, y, w, h;
private float dx, dy;
private Map<Class, Integer> colors;
public FieldDisplay(PApplet p, Simulator s) {
this(p, s.getField());
}
public FieldDisplay(PApplet p, Field f) {
this(p, f, 1, 1, 100, 100);
}
public FieldDisplay(PApplet p, Field f, int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.p = p;
this.f = f;
this.dx = (w / f.getWidth())/3;
this.dy = h / f.getHeight()+1;
colors = new LinkedHashMap<Class, Integer>();
}
public void drawField(Field f) {
Object organism;
Integer organismColor;
for (int i = 0; i < f.getWidth(); i++) {
for (int j = 0; j < f.getHeight(); j++) {
organism = f.getObjectAt(i, j);
if (organism != null) {
organismColor = getColor(organism.getClass());
p.fill(organismColor);
} else {
p.fill(this.EMPTY_COLOR);
}
p.rect(x + i * dx, y + j * dy, dx, dy);
}
}
}
public void setColor(Class organismClass, Integer color) {
colors.put(organismClass, color);
}
private Integer getColor(Class organismClass) {
Integer col = colors.get(organismClass);
if (col == null) { // no color defined for this class
return UNKNOWN_COLOR;
} else {
return col;
}
}
public Location gridLocationAt(float mx, float my) {
if (mx > x && mx < x + w && my > y && my < y+h) {
return new Location((int)Math.floor((mx-x)/dx),
(int)Math.floor((my-y)/dy));
} else return null;
}
}