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

📄 editableompoly.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/omGraphics/EditableOMPoly.java,v $// $RCSfile: EditableOMPoly.java,v $// $Revision: 1.9.2.4 $// $Date: 2006/01/05 15:20:42 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.omGraphics;import java.awt.Component;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.net.URL;import java.util.ArrayList;import java.util.Iterator;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JMenu;import javax.swing.JToggleButton;import javax.swing.JToolBar;import com.bbn.openmap.LatLonPoint;import com.bbn.openmap.gui.GridBagToolBar;import com.bbn.openmap.layer.util.stateMachine.State;import com.bbn.openmap.omGraphics.editable.GraphicEditState;import com.bbn.openmap.omGraphics.editable.GraphicSelectedState;import com.bbn.openmap.omGraphics.editable.PolyAddNodeState;import com.bbn.openmap.omGraphics.editable.PolyDeleteNodeState;import com.bbn.openmap.omGraphics.editable.PolyStateMachine;import com.bbn.openmap.omGraphics.editable.PolyUndefinedState;import com.bbn.openmap.proj.Projection;import com.bbn.openmap.util.Debug;/** * The EditableOMPoly encompasses an OMPoly, providing methods for * modifying or creating it. */public class EditableOMPoly extends EditableOMAbstractLine {    protected ArrayList polyGrabPoints;    protected OffsetGrabPoint gpo; // offset    protected OffsetGrabPoint gpm; // for grabbing the poly and    // moving    // it.    protected OMPoly poly;    /**     * Whether the poly is a polygon, as opposed to a polyline. If the     * poly color is not clear (OMColor.clear) then it will be a     * polygon. If it is clear, then it can be set as a polygon - it's     * otherwise assumed to be a polyline.     */    protected boolean manualEnclosed = false;    // We'll have to handle this differently...    public static int OFFSET_POINT_INDEX = -1;    /**     * Create the EditableOMPoly, setting the state machine to create     * the poly off of the gestures.     */    public EditableOMPoly() {        createGraphic(null);    }    /**     * Create an EditableOMPoly with the polyType and renderType     * parameters in the GraphicAttributes object.     */    public EditableOMPoly(GraphicAttributes ga) {        createGraphic(ga);    }    /**     * Create the EditableOMPoly with an OMPoly already defined, ready     * for editing.     *      * @param omp OMPoly that should be edited.     */    public EditableOMPoly(OMPoly omp) {        setGraphic(omp);    }    /**     * Create and initialize the state machine that interprets the     *      * modifying gestures/commands, as well as ititialize the grab     * points. Also allocates the grab point array needed by the     * EditableOMPoly.     */    public void init() {        Debug.message("eomg", "EditableOMPoly.init()");        setStateMachine(new PolyStateMachine(this));        gPoints = new GrabPoint[1];    }    /**     * Set the graphic within the state machine. If the graphic is     * null, then one shall be created, and located off screen until     * the gestures driving the state machine place it on the map.     */    public void setGraphic(OMGraphic graphic) {        init();        if (graphic instanceof OMPoly) {            poly = (OMPoly) graphic;            poly.setDoShapes(true);            stateMachine.setSelected();            setGrabPoints(poly);        } else {            createGraphic(null);        }    }    /**     * Method checks if the polygon should be enclosed, and then adds     * an addition point to the end of the polygon, setting the end     * point on top of the beginning point. The two points are     * OffsetGrabPoints that are tied to each other's position.     */    public boolean evaluateEnclosed() {        deletePoint();        boolean enclosed = false;        if (isEnclosed()) {            enclose(true);            enclosed = true;        }        return enclosed;    }    /**     * Method connects the last point to the first point. Make sure     * they are both OffsetGrabPoints. Return true if the points cover     * the same pixel and if they were successfully joined.     */    protected boolean syncEnclosed() {        try {            OffsetGrabPoint gb0 = (OffsetGrabPoint) polyGrabPoints.get(0);            OffsetGrabPoint ogb = (OffsetGrabPoint) polyGrabPoints.get(polyGrabPoints.size() - 1);            // Check to see if they are over the same point.            if (gb0.getX() == ogb.getX() && gb0.getY() == ogb.getY()) {                // Cross connect them...                gb0.addGrabPoint(ogb);                ogb.addGrabPoint(gb0);                return true;            }        } catch (ClassCastException cce) {        } catch (IndexOutOfBoundsException ioobe) {        }        return false;    }    /**     * Method disconnects the last point from the first point. Make     * sure they are both OffsetGrabPoints.     */    protected boolean unsyncEnclosed() {        try {            OffsetGrabPoint gb0 = (OffsetGrabPoint) polyGrabPoints.get(0);            OffsetGrabPoint ogb = (OffsetGrabPoint) polyGrabPoints.get(polyGrabPoints.size() - 1);            // disconnect them...            if (gb0.getX() == ogb.getX() && gb0.getY() == ogb.getY()) {                gb0.removeGrabPoint(ogb);                ogb.removeGrabPoint(gb0);                return true;            }        } catch (ClassCastException cce) {        } catch (ArrayIndexOutOfBoundsException aioobe) {        }        return false;    }    public void enclose(boolean e) {        setEnclosed(e);        if (polyGrabPoints == null) {            return;        }        OffsetGrabPoint gb0 = (OffsetGrabPoint) polyGrabPoints.get(0);        OffsetGrabPoint ogb;        if (e) {            // If they should be enclosed...            if (!syncEnclosed()) {                // And they are not already, then add a point, joined                // to the beginning.                ogb = new OffsetGrabPoint(gb0.getX(), gb0.getY());                // Add the new point to end of the poly                addPoint(ogb);                syncEnclosed();                repaint();            } // Else nothing to do...        } else {            // They shouldn't be hooked up, so check to see if they            // are, and disconnect if necessary.            if (unsyncEnclosed()) {                deletePoint(); // Delete attached duplicate point                repaint();            } // else nothing to do.        }    }    /**     * Set the flag to make the polygon enclosed, which automatically     * connects the last point with the first point.     */    public void setEnclosed(boolean set) {        manualEnclosed = set;    }    /**     * Returns whether the graphic will be a polygon, instead of a     * polyline.     */    public boolean isEnclosed() {        return manualEnclosed;    }    /**     * Create and set the graphic within the state machine. The     * GraphicAttributes describe the type of poly to create.     */    public void createGraphic(GraphicAttributes ga) {        init();        stateMachine.setUndefined();        int renderType = OMGraphic.RENDERTYPE_LATLON;        int lineType = OMGraphic.LINETYPE_GREATCIRCLE;        if (ga != null) {            renderType = ga.getRenderType();            lineType = ga.getLineType();        }        if (Debug.debugging("eomg")) {            Debug.output("EditableOMPoly.createGraphic(): rendertype = "                    + renderType);        }        if (lineType == OMGraphic.LINETYPE_UNKNOWN) {            lineType = OMGraphic.LINETYPE_GREATCIRCLE;            ga.setLineType(OMGraphic.LINETYPE_GREATCIRCLE);        }        this.poly = (OMPoly) createGraphic(renderType, lineType);        if (ga != null) {            ga.setRenderType(poly.getRenderType());            ga.setTo(poly);        }    }    /**     * Extendable method to create specific subclasses of OMPolys.     */    public OMGraphic createGraphic(int renderType, int lineType) {        OMGraphic g = null;        switch (renderType) {        case (OMGraphic.RENDERTYPE_LATLON):            g = new OMPoly(new float[0], OMGraphic.RADIANS, lineType);            break;        case (OMGraphic.RENDERTYPE_OFFSET):            g = new OMPoly(90f, -180f, new int[0], OMPoly.COORDMODE_ORIGIN);            break;        default:            g = new OMPoly(new int[0]);        }        ((OMPoly) g).setDoShapes(true);        return g;    }    /**     * Get the OMGraphic being created/modified by the EditableOMPoly.     */    public OMGraphic getGraphic() {        return poly;    }    /**     * Attach to the Moving OffsetGrabPoint so if it moves, it will     * move this EditableOMGraphic with it. EditableOMGraphic version     * doesn't do anything, each subclass has to decide which of its     * OffsetGrabPoints should be attached to it.     */    public void attachToMovingGrabPoint(OffsetGrabPoint gp) {        gp.addGrabPoint(gpo);    }    /**     * Detach from a Moving OffsetGrabPoint. The EditableOMGraphic     * version doesn't do anything, each subclass should remove     * whatever GrabPoint it would have attached to an     * OffsetGrabPoint.     */    public void detachFromMovingGrabPoint(OffsetGrabPoint gp) {        gp.removeGrabPoint(gpo);    }    /**     * Set the GrabPoint that is in the middle of being modified, as a     * result of a mouseDragged event, or other selection process.     */    public void setMovingPoint(GrabPoint gp) {        super.setMovingPoint(gp);        gpm = null;    }    /**     * Given a MouseEvent, find a GrabPoint that it is touching, and     * set the moving point to that GrabPoint. Called when a     * MouseEvent happens, and you want to find out if a GrabPoint     * should be used to make modifications to the graphic or its     * position.     *      * @param e MouseEvent     * @return GrabPoint that is touched by the MouseEvent, null if     *         none are.     */    public GrabPoint getMovingPoint(MouseEvent e) {        GrabPoint gb = super.getMovingPoint(e);        // Since there may be an extra point enclosing the polygon, we        // want to make sure that the start of the polygon is

⌨️ 快捷键说明

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