-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarthquakeMarker.java
196 lines (151 loc) · 4.96 KB
/
EarthquakeMarker.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
package module6;
import de.fhpotsdam.unfolding.data.PointFeature;
import processing.core.PConstants;
import processing.core.PGraphics;
/** Implements a visual marker for earthquakes on an earthquake map
*
* @author UC San Diego Intermediate Software Development MOOC team
*
*/
// TODO: Implement the comparable interface
public abstract class EarthquakeMarker extends CommonMarker implements Comparable<EarthquakeMarker> {
// Did the earthquake occur on land? This will be set by the subclasses.
protected boolean isOnLand;
// The radius of the Earthquake marker
// You will want to set this in the constructor, either
// using the thresholds below, or a continuous function
// based on magnitude.
protected float radius;
// constants for distance
protected static final float kmPerMile = 1.6f;
/** Greater than or equal to this threshold is a moderate earthquake */
public static final float THRESHOLD_MODERATE = 5;
/** Greater than or equal to this threshold is a light earthquake */
public static final float THRESHOLD_LIGHT = 4;
/** Greater than or equal to this threshold is an intermediate depth */
public static final float THRESHOLD_INTERMEDIATE = 70;
/** Greater than or equal to this threshold is a deep depth */
public static final float THRESHOLD_DEEP = 300;
// ADD constants for colors
// abstract method implemented in derived classes
public abstract void drawEarthquake(PGraphics pg, float x, float y);
// constructor
public EarthquakeMarker (PointFeature feature)
{
super(feature.getLocation());
// Add a radius property and then set the properties
java.util.HashMap<String, Object> properties = feature.getProperties();
float magnitude = Float.parseFloat(properties.get("magnitude").toString());
properties.put("radius", 2*magnitude );
setProperties(properties);
this.radius = 2.75f*getMagnitude();
}
// TODO: Add the method:
public int compareTo(EarthquakeMarker marker) {
float diff = this.getMagnitude() - marker.getMagnitude();
if (diff > 0) {
return -1;
} else if (diff < 0) {
return 1;
} else {
return 0;
}
}
// calls abstract method drawEarthquake and then checks age and draws X if needed
@Override
public void drawMarker(PGraphics pg, float x, float y) {
// save previous styling
pg.pushStyle();
// determine color of marker from depth
colorDetermine(pg);
// call abstract method implemented in child class to draw marker shape
drawEarthquake(pg, x, y);
// IMPLEMENT: add X over marker if within past day
String age = getStringProperty("age");
if ("Past Hour".equals(age) || "Past Day".equals(age)) {
pg.strokeWeight(2);
int buffer = 2;
pg.line(x-(radius+buffer),
y-(radius+buffer),
x+radius+buffer,
y+radius+buffer);
pg.line(x-(radius+buffer),
y+(radius+buffer),
x+radius+buffer,
y-(radius+buffer));
}
// reset to previous styling
pg.popStyle();
}
/** Show the title of the earthquake if this marker is selected */
public void showTitle(PGraphics pg, float x, float y)
{
String title = getTitle();
pg.pushStyle();
pg.rectMode(PConstants.CORNER);
pg.stroke(110);
pg.fill(255,255,255);
pg.rect(x, y + 15, pg.textWidth(title) +6, 18, 5);
pg.textAlign(PConstants.LEFT, PConstants.TOP);
pg.fill(0);
pg.text(title, x + 3 , y +18);
pg.popStyle();
}
/**
* Return the "threat circle" radius, or distance up to
* which this earthquake can affect things, for this earthquake.
* DISCLAIMER: this formula is for illustration purposes
* only and is not intended to be used for safety-critical
* or predictive applications.
*/
public double threatCircle() {
double miles = 20.0f * Math.pow(1.8, 2*getMagnitude()-5);
double km = (miles * kmPerMile);
return km;
}
// determine color of marker from depth
// We use: Deep = red, intermediate = mid red, shallow = yellow
private void colorDetermine(PGraphics pg) {
float depth = getDepth();
if (depth < THRESHOLD_INTERMEDIATE) {
pg.fill(252, 161, 1, 100);//light orange
} else if(depth > THRESHOLD_INTERMEDIATE) {
pg.fill(199, 0, 57, 100);//mid red
}
else if (depth < THRESHOLD_LIGHT) {
pg.fill(255, 255, 240, 100);//light yellow
}
else if(depth < THRESHOLD_DEEP){
pg.fill(255,30,0, 100); //red
} else {
pg.fill(252, 9, 1, 100);//deep red
}
}
/** toString
* Returns an earthquake marker's string representation
* @return the string representation of an earthquake marker.
*/
public String toString()
{
return getTitle();
}
/*
* getters for earthquake properties
*/
public float getMagnitude() {
return Float.parseFloat(getProperty("magnitude").toString());
}
public float getDepth() {
return Float.parseFloat(getProperty("depth").toString());
}
public String getTitle() {
return (String) getProperty("title");
}
public float getRadius() {
return Float.parseFloat(getProperty("radius").toString());
}
public boolean isOnLand()
{
return isOnLand;
}
}