📄 flatworldearthquakes.java
字号:
PatternFactory.createPattern(PatternFactory.PATTERN_CIRCLE, .8f, Color.BLUE),
PatternFactory.createPattern(PatternFactory.PATTERN_CIRCLE, .8f, Color.GRAY),
PatternFactory.createPattern(PatternFactory.PATTERN_CIRCLE, .8f, Color.BLACK),
};
private Color eqColors[] =
{
Color.RED,
Color.ORANGE,
Color.YELLOW,
Color.GREEN,
Color.BLUE,
Color.GRAY,
Color.BLACK,
};
private void addEarthquake(RenderableLayer layer, Earthquake earthquake)
{
if (eqAttributes == null)
{
// Init default attributes for all eq
eqAttributes = new AnnotationAttributes();
eqAttributes.setLeader(FrameFactory.LEADER_NONE);
eqAttributes.setDrawOffset(new Point(0, -16));
eqAttributes.setSize(new Dimension(32, 32));
eqAttributes.setBorderWidth(0);
eqAttributes.setCornerRadius(0);
eqAttributes.setBackgroundColor(new Color(0, 0, 0, 0));
}
EqAnnotation ea = new EqAnnotation(earthquake, eqAttributes);
int days = 6;
if (earthquake.date != null) {
// Compute days since
Date now = new Date();
days = (int) ((now.getTime() - earthquake.date.getTime()) / javax.management.timer.Timer.ONE_DAY);
// Update latestEq
if (this.latestEq != null)
{
if (this.latestEq.earthquake.date.getTime() < earthquake.date.getTime())
this.latestEq = ea;
}
else
this.latestEq = ea;
}
ea.getAttributes().setImageSource(eqIcons[Math.min(days, eqIcons.length - 1)]);
ea.getAttributes().setTextColor(eqColors[Math.min(days, eqColors.length - 1)]);
ea.getAttributes().setScale(earthquake.magnitude / 10);
layer.addRenderable(ea);
}
private static Node findChildByName(Node parent, String localName)
{
NodeList children = parent.getChildNodes();
if (children == null || children.getLength() < 1)
return null;
for (int i = 0; i < children.getLength(); i++)
{
String ln = children.item(i).getNodeName();
if (ln != null && ln.equals(localName))
return children.item(i);
}
return null;
}
private void applyFilter(double minMagnitude)
{
this.latestEq = null;
setBlinker(null);
setLatestLabel(null);
Iterable<Renderable> renderables = eqLayer.getRenderables();
for (Renderable r : renderables)
{
if (r instanceof EqAnnotation)
{
EqAnnotation ea = (EqAnnotation)r;
ea.getAttributes().setVisible(ea.earthquake.magnitude >= minMagnitude);
if (ea.getAttributes().isVisible())
{
if (this.latestEq != null)
{
if (this.latestEq.earthquake.date != null && ea.earthquake.date != null)
if (this.latestEq.earthquake.date.getTime() < ea.earthquake.date.getTime())
this.latestEq = ea;
}
else
this.latestEq = ea;
}
}
}
setBlinker(this.latestEq);
setLatestLabel(this.latestEq);
this.getWwd().repaint();
}
private class Earthquake
{
public String title;
public String summary;
public Position position;
public double elevation;
public Date date;
public double magnitude;
public String link;
public Earthquake(Node entry)
{
Node node = findChildByName(entry, "title");
if(node != null)
{
this.title = node.getTextContent();
this.magnitude = Double.parseDouble(title.split(",")[0].substring(2));
}
node = findChildByName(entry, "georss:point");
if (node != null)
{
String pointString = node.getTextContent();
String[] coord = pointString.split(" ");
this.position = Position.fromDegrees(Double.parseDouble(coord[0]), Double.parseDouble(coord[1]), 0);
}
node = findChildByName(entry, "georss:elev");
if (node != null)
this.elevation = Double.parseDouble(node.getTextContent());
node = findChildByName(entry, "summary");
if (node != null)
this.summary = node.getTextContent();
node = findChildByName(entry, "link");
if (node != null)
this.link = node.getAttributes().getNamedItem("href").getTextContent();
node = findChildByName(entry, "updated");
if (node != null)
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
try
{
this.date = df.parse(node.getTextContent().replaceAll("[TZ]", " ").trim());
}
catch (Exception e) {}
}
}
}
private class EqAnnotation extends GlobeAnnotation
{
public Earthquake earthquake;
public EqAnnotation(Earthquake earthquake, AnnotationAttributes defaults)
{
super ("", earthquake.position, defaults);
this.earthquake = earthquake;
}
protected void applyScreenTransform(DrawContext dc, int x, int y, int width, int height, double scale)
{
double finalScale = scale * this.computeScale(dc);
GL gl = dc.getGL();
gl.glTranslated(x, y, 0);
gl.glScaled(finalScale, finalScale, 1);
}
// Override annotation drawing for a simple circle
private DoubleBuffer shapeBuffer;
protected void doDraw(DrawContext dc, int width, int height, double opacity, Position pickPosition)
{
// Draw colored circle around screen point - use annotation's text color
if (dc.isPickingMode())
{
this.bindPickableObject(dc, pickPosition);
}
this.setDrawColor(dc, this.getAttributes().getTextColor(), 0.6 * opacity,
OGLStateSupport.COLOR_PREMULTIPLIED_ALPHA);
// Draw 32x32 shape from its bottom left corner
int size = 32;
if (this.shapeBuffer == null)
this.shapeBuffer = FrameFactory.createShapeBuffer(FrameFactory.SHAPE_ELLIPSE, size, size, 0, null);
dc.getGL().glTranslated(-size/2, -size/2, 0);
FrameFactory.drawBuffer(dc, GL.GL_TRIANGLE_FAN, this.shapeBuffer);
}
}
private class Blinker
{
private EqAnnotation annotation;
private double initialScale, initialOpacity;
private int steps = 10;
private int step = 0;
private int delay = 100;
private Timer timer;
private Blinker(EqAnnotation ea)
{
this.annotation = ea;
this.initialScale = this.annotation.getAttributes().getScale();
this.initialOpacity = this.annotation.getAttributes().getOpacity();
this.timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//System.out.println("Blink: " + annotation.earthquake.title + " " + step + " " + initialScale + " " + initialOpacity);
annotation.getAttributes().setScale(initialScale * (1f + 7f * ((float)step / (float)steps)));
annotation.getAttributes().setOpacity(initialOpacity * (1f - ((float)step / (float)steps)));
step = step == steps ? 0 : step + 1;
getWwd().repaint();
}
});
start();
}
private void stop()
{
timer.stop();
step = 0;
this.annotation.getAttributes().setScale(initialScale);
this.annotation.getAttributes().setOpacity(initialOpacity);
}
private void start()
{
timer.start();
}
}
} // End AppFrame
// --- Main -------------------------------------------------------------------------
public static void main(String[] args)
{
// Adjust configuration values before instanciation
Configuration.setValue(AVKey.INITIAL_LATITUDE, 0);
Configuration.setValue(AVKey.INITIAL_LONGITUDE, 0);
Configuration.setValue(AVKey.INITIAL_ALTITUDE, 50e6);
Configuration.setValue(AVKey.GLOBE_CLASS_NAME, EarthFlat.class.getName());
Configuration.setValue(AVKey.VIEW_CLASS_NAME, FlatOrbitView.class.getName());
ApplicationTemplate.start("World Wind USGS Earthquakes M 2.5+ - 7 days", AppFrame.class);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -