⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flatworldearthquakes.java

📁 world wind java sdk 源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
Copyright (C) 2001, 2008 United States Government
as represented by the Administrator of the
National Aeronautics and Space Administration.
All Rights Reserved.
*/
package gov.nasa.worldwind.examples;

import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.exception.WWRuntimeException;
import gov.nasa.worldwind.geom.*;
import gov.nasa.worldwind.globes.*;
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.util.*;
import gov.nasa.worldwind.view.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.media.opengl.GL;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.xml.parsers.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.text.*;
import java.util.*;
import java.util.logging.Level;

/**
 * Using the EarthFlat and FlatOrbitView to display USGS latest earthquakes rss feed.
 *
 * @author Patrick Murris
 * @version $Id: FlatWorldEarthquakes.java 10925 2009-05-06 22:22:50Z dcollins $
 */
public class FlatWorldEarthquakes extends ApplicationTemplate
{
    public static class AppFrame extends ApplicationTemplate.AppFrame
    {
        private RenderableLayer eqLayer;
        private EqAnnotation mouseEq, latestEq;
        private GlobeAnnotation tooltipAnnotation;
        private JButton downloadButton;
        private JLabel statusLabel, latestLabel;
        private Date lastUpdate;
        private Blinker blinker;
        private Timer updater;
        private Date lastUpdaterEvent;
        private JComboBox magnitudeCombo;

        public AppFrame()
        {
            super(true, true, false);

            // Change atmosphere SkyGradientLayer for SkyColorLayer
            // and set worldmap and compass max active altitude
            LayerList layers = this.getWwd().getModel().getLayers();
            for(int i = 0; i < layers.size(); i++)
            {
                if(layers.get(i) instanceof SkyGradientLayer)
                    layers.set(i, new SkyColorLayer());
                else if(layers.get(i) instanceof WorldMapLayer)
                    ((WorldMapLayer)layers.get(i)).setMaxActiveAltitude(20e6);
                else if(layers.get(i) instanceof CompassLayer)
                    ((CompassLayer)layers.get(i)).setMaxActiveAltitude(20e6);
            }

            // Init tooltip annotation
            this.tooltipAnnotation = new GlobeAnnotation("", Position.fromDegrees(0, 0, 0));
            Font font = Font.decode("Arial-Plain-16");
            this.tooltipAnnotation.getAttributes().setFont(font);
            this.tooltipAnnotation.getAttributes().setSize(new Dimension(270, 0));
            this.tooltipAnnotation.getAttributes().setDistanceMinScale(1);
            this.tooltipAnnotation.getAttributes().setDistanceMaxScale(1);
            this.tooltipAnnotation.getAttributes().setVisible(false);
            this.tooltipAnnotation.setAlwaysOnTop(true);

            // Add control panels
            JPanel controls = new JPanel();
            controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
            // Add earthquakes view control panel
            controls.add(makeEarthquakesPanel());
            // Add flat world projection control panel
            controls.add(new FlatWorldPanel(this.getWwd()));
            this.getLayerPanel().add(controls,  BorderLayout.SOUTH);

            // Add select listener for earthquake picking
            this.getWwd().addSelectListener(new SelectListener(){
                public void selected(SelectEvent event){
                    if (event.getEventAction().equals(SelectEvent.ROLLOVER))
                        highlight(event.getTopObject());
                }});

            // Add click-and-go select listener for earthquakes
            this.getWwd().addSelectListener(new ClickAndGoSelectListener(
                    this.getWwd(), EqAnnotation.class, 1000e3));

            // Add updater timer
            this.updater = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent event)
                {
                    if (lastUpdaterEvent == null)
                        lastUpdaterEvent = new Date();
                    Date now = new Date();
                    long delay = javax.management.timer.Timer.ONE_MINUTE * 5;
                    long elapsed = now.getTime() - lastUpdaterEvent.getTime();
                    if (elapsed >= delay)
                    {
                        // Auto download every 5 minutes
                        lastUpdaterEvent = new Date();
                        downloadButton.setText("Update");
                        startEarthquakeDownload();
                    }
                    else
                    {
                        // Display remaining time in button text
                        long remain = delay - elapsed;
                        int min = (int)Math.floor((double)remain / javax.management.timer.Timer.ONE_MINUTE);
                        int sec = (int)((remain - min * javax.management.timer.Timer.ONE_MINUTE) / javax.management.timer.Timer.ONE_SECOND);
                        downloadButton.setText(String.format("Update (in %1$02d:%2$02d)", min, sec));
                    }
                }
            });
            this.updater.start();
            
            // Download earthquakes
            startEarthquakeDownload();
        }

        private void highlight(Object o)
        {
            if (this.mouseEq == o)
                return; // same thing selected

            if (this.mouseEq != null)
            {
                this.mouseEq.getAttributes().setHighlighted(false);
                this.mouseEq = null;
                this.tooltipAnnotation.getAttributes().setVisible(false);
            }

            if (o != null && o instanceof EqAnnotation)
            {
                this.mouseEq = (EqAnnotation) o;
                this.mouseEq.getAttributes().setHighlighted(true);
                this.tooltipAnnotation.setText("<p><b>" + this.mouseEq.earthquake.title + "</b></p>" + composeElapsedString(this.mouseEq) + "<br />" + this.mouseEq.earthquake.summary);
                this.tooltipAnnotation.setPosition(this.mouseEq.earthquake.position);
                this.tooltipAnnotation.getAttributes().setVisible(true);
                this.getWwd().repaint();
            }
        }

        private void setBlinker(EqAnnotation ea)
        {
            if (this.blinker != null)
            {
                this.blinker.stop();
                this.getWwd().repaint();
            }

            if (ea == null)
                return;

            this.blinker = new Blinker(ea);
        }

        private void setLatestLabel(EqAnnotation ea)
        {
            this.latestLabel.setText("");
            if (ea != null)
            {
                String htmlText = "<html>" + composeElapsedString(ea) + "<p><b>" + ea.earthquake.title + "</b></p>" + ea.earthquake.summary + "</html>";
                htmlText = htmlText.replaceAll("(?i)<img\\s?.*?>", "\n");  // Remove <img> tags
                this.latestLabel.setText(htmlText);
            }
        }

        private String composeElapsedString(EqAnnotation ea)
        {
            String s = "";
            if (ea.earthquake.date != null)
            {
                Date now = new Date();
                long elapsed = now.getTime() - ea.earthquake.date.getTime();
                long days = elapsed / javax.management.timer.Timer.ONE_DAY;
                elapsed -= days * javax.management.timer.Timer.ONE_DAY;
                long hours = elapsed / javax.management.timer.Timer.ONE_HOUR;
                elapsed -= hours * javax.management.timer.Timer.ONE_HOUR;
                long minutes = elapsed / javax.management.timer.Timer.ONE_MINUTE;
                if(days > 0)
                {
                    s = days + (days > 1 ? " days" : " day") + (hours > 0 ? " and " + hours + (hours > 1 ? " hours" : " hour") : "");
                }
                else
                {
                    if (hours > 0)
                        s = hours + (hours > 1 ? " hours" : " hour") + (hours < 12 ? " and " + minutes + (minutes > 1 ? " minutes" : " minute") : "");
                    else
                        s = minutes + (minutes > 1 ? " minutes" : " minute");
                }
                s += " ago";
            }
            return s;
        }

        private JPanel makeEarthquakesPanel()
        {
            JPanel controlPanel = new JPanel();
            controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));

            // Zoom on latest button
            JPanel zoomPanel = new JPanel(new GridLayout(0, 1, 0, 0));
            zoomPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
            JButton btZoom = new JButton("Zoom on latest");
            btZoom.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if (latestEq != null)
                    {
                        Position targetPos = latestEq.earthquake.position;
                        OrbitView view = (OrbitView) getWwd().getView();
                        Globe globe = getWwd().getModel().getGlobe();
                        // Use a PanToIterator zoom onto the latest eq postion
                        view.applyStateIterator(FlyToOrbitViewStateIterator.createPanToIterator(
                                // The elevation component of 'targetPos' here is not the surface elevation,
                                // so we ignore it when specifying the view center position.
                                view, globe, new Position(targetPos, 0),
                                Angle.ZERO, Angle.ZERO, 1000e3));
                    }

                }
            });
            zoomPanel.add(btZoom);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -