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

📄 draggingshapes.java

📁 world wind java sdk 源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*Copyright (C) 2001, 2006 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.examples;import gov.nasa.worldwind.*;import gov.nasa.worldwind.event.*;import gov.nasa.worldwind.geom.*;import gov.nasa.worldwind.layers.*;import gov.nasa.worldwind.pick.PickedObjectList;import gov.nasa.worldwind.render.*;import gov.nasa.worldwind.util.WWIO;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import java.util.List;/** * @author tag * @version $Id: DraggingShapes.java 10300 2009-04-17 18:02:57Z dcollins $ */public class DraggingShapes extends ApplicationTemplate{    private static class AppFrame extends ApplicationTemplate.AppFrame    {        protected RenderableLayer shapeLayer;        protected SurfaceShape lastHighlitShape;        protected ShapeAttributes lastShapeAttributes;        protected BasicDragger dragger;        protected boolean drawWireframeInterior = false;        protected boolean drawWireframeExterior = false;        public AppFrame()        {            this.shapeLayer = this.buildShapesLayer();            this.dragger = new BasicDragger(getWwd());            insertBeforeCompass(this.getWwd(), this.shapeLayer);            this.getLayerPanel().update(this.getWwd());            this.getWwd().addSelectListener(new SelectListener()            {                public void selected(SelectEvent event)                {                    if (lastHighlitShape != null                        && (event.getTopObject() == null || !event.getTopObject().equals(lastHighlitShape)))                    {                        lastHighlitShape.setAttributes(lastShapeAttributes);                        lastHighlitShape = null;                    }                    // Have rollover events highlight the rolled-over object.                    if (event.getEventAction().equals(SelectEvent.ROLLOVER) && !dragger.isDragging())                    {                        AppFrame.this.highlight(event.getTopObject());                    }                    // Have drag events drag the selected object.                    else if (event.getEventAction().equals(SelectEvent.DRAG_END)                        || event.getEventAction().equals(SelectEvent.DRAG))                    {                        // Delegate dragging computations to a dragger.                        dragger.selected(event);                        // We missed any roll-over events while dragging, so highlight any under the cursor now,                        // or de-highlight the dragged shape if it's no longer under the cursor.                        if (event.getEventAction().equals(SelectEvent.DRAG_END))                        {                            PickedObjectList pol = getWwd().getObjectsAtCurrentPosition();                            if (pol != null)                            {                                AppFrame.this.highlight(pol.getTopObject());                                AppFrame.this.getWwd().repaint();                            }                        }                    }                }            });            JMenuBar menuBar = new JMenuBar();            {                JMenu menu = new JMenu("File");                JMenuItem item = new JMenuItem("Open");                item.addActionListener(new ActionListener()                {                    public void actionPerformed(ActionEvent e)                    {                        loadObjects();                    }                });                menu.add(item);                item = new JMenuItem("Save");                item.addActionListener(new ActionListener()                {                    public void actionPerformed(ActionEvent e)                    {                        saveObjects();                    }                });                menu.add(item);                menuBar.add(menu);            }            this.setJMenuBar(menuBar);        }        protected void saveObjects()        {            JFileChooser fc = new JFileChooser();            fc.setCurrentDirectory(new File(Configuration.getUserHomeDirectory()));            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);            fc.setMultiSelectionEnabled(false);            int status = fc.showSaveDialog(this);            if (status != JFileChooser.APPROVE_OPTION)                return;            File dir = fc.getSelectedFile();            if (dir == null)                return;            if (!dir.exists())            {                //noinspection ResultOfMethodCallIgnored                dir.mkdirs();            }            java.text.DecimalFormat f = new java.text.DecimalFormat("####");            f.setMinimumIntegerDigits(4);            int counter = 0;            for (Renderable r : this.shapeLayer.getRenderables())            {                if (!(r instanceof Restorable))                    continue;                String xmlString = ((Restorable) r).getRestorableState();                if (xmlString == null)                    continue;                File file = new File(dir, r.getClass().getName() + "-" + f.format(counter) + ".xml");                WWIO.writeTextFile(xmlString, file);                counter++;            }        }        protected void loadObjects()        {            JFileChooser fc = new JFileChooser();            fc.setCurrentDirectory(new File(Configuration.getUserHomeDirectory()));            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);            fc.setMultiSelectionEnabled(false);            int status = fc.showOpenDialog(this);            if (status != JFileChooser.APPROVE_OPTION)                return;            File dir = fc.getSelectedFile();            if (dir == null)                return;            FilenameFilter filter = new FilenameFilter()            {                public boolean accept(File dir, String name)                {                    return name.startsWith("gov.nasa.worldwind.") && name.endsWith(".xml");                }            };            List<Renderable> renderableList = new ArrayList<Renderable>();            for (File file : dir.listFiles(filter))            {                String[] name = file.getName().split("-");                try                {                    Class c = Class.forName(name[0]);                    String xmlString = WWIO.readTextFile(file);                    Restorable restorable = (Restorable) c.newInstance();                    restorable.restoreState(xmlString);                    if (restorable instanceof Renderable)                    {                        renderableList.add((Renderable) restorable);                    }                }                catch (Exception e)                {                    e.printStackTrace();                }            }            this.shapeLayer.removeAllRenderables();            this.shapeLayer.addRenderables(renderableList);        }        protected void highlight(Object o)        {            // Same shape selected.            if (o == this.lastHighlitShape)                return;            if (this.lastHighlitShape == null && o instanceof AbstractSurfaceShape)            {                this.lastHighlitShape = (AbstractSurfaceShape) o;                this.lastShapeAttributes = this.lastHighlitShape.getAttributes();                ShapeAttributes selectedAttributes = this.getHighlightAttributes(this.lastShapeAttributes);                this.lastHighlitShape.setAttributes(selectedAttributes);            }        }        protected ShapeAttributes getHighlightAttributes(ShapeAttributes attributes)        {            ShapeAttributes selectedAttributes = new BasicShapeAttributes(attributes);            if (selectedAttributes.isDrawInterior())            {                selectedAttributes.setInteriorMaterial(Material.WHITE);            }            else if (selectedAttributes.isDrawOutline())            {                selectedAttributes.setOutlineMaterial(Material.WHITE);            }            return selectedAttributes;        }        protected RenderableLayer buildShapesLayer()        {            SurfaceShapeLayer layer = new SurfaceShapeLayer();            // Create a yellow, semi-transparent surface shape over Mt. Shasta.            AbstractSurfaceShape shape = new SurfaceSector(new Sector(                Angle.fromDegrees(41.0), Angle.fromDegrees(41.6),                Angle.fromDegrees(-122.5), Angle.fromDegrees(-121.7)));            this.setupFilledShape(shape, layer, Color.YELLOW);            // Create a turquoise, semi-transparent surface shape over Lake Tahoe.            shape = new SurfaceSector(new Sector(                Angle.fromDegrees(38.9), Angle.fromDegrees(39.3),                Angle.fromDegrees(-120.2), Angle.fromDegrees(-119.9)));            this.setupFilledShape(shape, layer, Color.CYAN);            // Create an ellipse over the center of the States.            shape = new SurfaceEllipse(LatLon.fromDegrees(38, -104), 1.5e5, 1e5, Angle.fromDegrees(15));            this.setupFilledShape(shape, layer, Color.CYAN);            // Create a quadrilateral over  the States.            shape = new SurfaceQuad(LatLon.fromDegrees(42, -104), 1e5, 1.3e5, Angle.fromDegrees(20));            this.setupFilledShape(shape, layer, Color.ORANGE);            // Create a square over the  States.

⌨️ 快捷键说明

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