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

📄 pilotpath.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
字号:
// **********************************************************************// // <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/j3d/com/bbn/openmap/plugin/pilot/PilotPath.java,v $// $RCSfile: PilotPath.java,v $// $Revision: 1.3.2.1 $// $Date: 2004/10/14 18:26:34 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.plugin.pilot;import java.awt.*;import javax.swing.*;import com.bbn.openmap.omGraphics.*;import com.bbn.openmap.LatLonPoint;import com.bbn.openmap.proj.*;import com.bbn.openmap.util.Debug;import com.bbn.openmap.MapHandler;import javax.media.j3d.*;import javax.vecmath.*;import com.bbn.openmap.tools.j3d.*;/** * The PilotPath is a definition of a path that a Java 3D window will * take. This is a demonstration class that will have improvements as * time goes on. Right now, height does not have an effect on the * view. * <P> *  * The PlugIn palette lets the user use the OMDrawingTool to define * the path. The path also provides a button on the palette to launch * the J3D viewer. */public class PilotPath extends Pilot implements NavBehaviorProvider {    float[] pathPoints = null;    OMPoly poly = null;    int pathIndex = 0;    float currentSegDist = 0f;    float nextSegOffset = 0f;    float rate = Length.METER.toRadians(10000);    protected boolean DEBUG = false;    /**     * Define a path, with the radius and isOval referring to the     * marker for marking the pilot's position on the path.     */    public PilotPath(OMPoly path, int radius, boolean isOval) {        super(0f, 0f, radius, isOval);        setPoly(path);        DEBUG = Debug.debugging("pilot");        setHeight(10.3f);    }    /**     * Tell the pilot to move along the path. The factor is not     * currently used.     */    public void move(float factor) {        if (!stationary) {            moveAlong();        }    }    /**     * Returns the coordinates for the current poly segment.     */    public float[] getSegmentCoordinates(int currentPathIndex) {        float[] latlons = new float[4];        if (pathIndex > pathPoints.length - 2 || pathIndex < 0) {            pathIndex = 0;        }        if (pathPoints != null && pathPoints.length >= 4) {            int la1 = pathIndex;            int lo1 = pathIndex + 1;            int la2 = pathIndex + 2;            int lo2 = pathIndex + 3;            if (lo2 >= pathPoints.length) {                if (poly.isPolygon()) {                    if (DEBUG)                        Debug.output("PilotPath.moveAlong(): index too big, wrapping... ");                    la2 = 0;                    lo2 = 1;                } else {                    pathIndex = 0;                    if (DEBUG)                        Debug.output("PilotPath.moveAlong(): index too big, no wrapping, starting over... ");                    return getSegmentCoordinates(pathIndex);                }            }            latlons[0] = pathPoints[la1];            latlons[1] = pathPoints[lo1];            latlons[2] = pathPoints[la2];            latlons[3] = pathPoints[lo2];        }        return latlons;    }    /**     * Figures out the next position of the pilot, given the distance     * the pilot should move for this turn.     */    public void moveAlong() {        if (DEBUG) {            Debug.output("PilotPath.moveAlong(): segment " + (pathIndex / 2)                    + " of " + (pathPoints.length / 2));        }        float azimuth;        LatLonPoint newPoint;        float[] latlons = getSegmentCoordinates(pathIndex);        float segLength = GreatCircle.spherical_distance(latlons[0],                latlons[1],                latlons[2],                latlons[3]);        if (DEBUG) {            Debug.output("PilotPath.moveAlong(): segment Length " + segLength                    + ", and already have " + currentSegDist + " of it.");        }        float needToTravel = rate;        int originalPathIndex = pathIndex;        int loopingTimes = 0;        while (needToTravel >= segLength - currentSegDist) {            needToTravel -= (segLength - currentSegDist);            currentSegDist = 0f;            pathIndex += 2;            // Move to the next segment of the poly            if (DEBUG) {                Debug.output("PilotPath to next segment(" + (pathIndex / 2)                        + "), need to travel " + needToTravel);            }            latlons = getSegmentCoordinates(pathIndex);            if (pathIndex == originalPathIndex) {                loopingTimes++;                if (loopingTimes > 1) {                    if (DEBUG)                        Debug.output("PilotPath looping on itself, setting to stationary");                    setStationary(true);                    return;                }            }            segLength = GreatCircle.spherical_distance(latlons[0],                    latlons[1],                    latlons[2],                    latlons[3]);        }        if (DEBUG) {            Debug.output("Moving PilotPath within current(" + (pathIndex / 2)                    + ") segment, segLength: " + segLength + ", ntt: "                    + needToTravel);        }        // Staying on this segment, just calculate where the        // next point on the segment is.        azimuth = GreatCircle.spherical_azimuth(latlons[0],                latlons[1],                latlons[2],                latlons[3]);        newPoint = GreatCircle.spherical_between(latlons[0],                latlons[1],                currentSegDist + needToTravel,                azimuth);        setLat(newPoint.getLatitude());        setLon(newPoint.getLongitude());        currentSegDist = GreatCircle.spherical_distance(latlons[0],                latlons[1],                newPoint.radlat_,                newPoint.radlon_);        // OK, now move the camera accordingly...        if (DEBUG)            Debug.output("moveAlong: azimuth = " + azimuth);        if (viewProjection == null) {            return;        }        Point newLoc = viewProjection.forward(newPoint);        if (DEBUG)            Debug.output(newLoc.toString() + ", compared with lastX, lastY: "                    + lastX + ", " + lastY + ", scaleFactor= " + scaleFactor);        double centerXOffset = (double) (newLoc.getX()) * scaleFactor;        double centerYOffset = (double) (newLoc.getY()) * scaleFactor;        Vector3d translate = new Vector3d();        // 0f can be changed to account for any height change.        translate.set(centerXOffset - lastX, 0, centerYOffset - lastY);        lastX = centerXOffset;        lastY = centerYOffset;        if (DEBUG)            Debug.output("PP moving: " + translate);        //         translateTransform.set(scaleFactor, translate);        //         cameraTransformGroup.getTransform(translateTransform);        //         Transform3D toMove = new Transform3D();        //         toMove.setTranslation(translate);        //         translateTransform.mul(toMove);        //         cameraTransformGroup.setTransform(translateTransform);        if (platformBehavior != null) {            platformBehavior.doMove(translate);            if (lastAzimuth != azimuth) {                platformBehavior.doLookY(lastAzimuth - azimuth);                lastAzimuth = azimuth;            }        }    }    protected Transform3D translateTransform = new Transform3D();    protected Projection viewProjection;    protected TransformGroup cameraTransformGroup;    protected float scaleFactor = 1f;    protected double lastX;    protected double lastY;    protected double lastAzimuth = 0;    /**     * Standard generate method, generating all the OMGraphics with     * the current position.     */    public boolean generate(Projection p) {        // At least try to keep the current version, in case things        // get set up with the 3D viewer out of order.        viewProjection = p;        boolean ret = super.generate(p);        if (poly != null) {            poly.generate(p);        }        return ret;    }    public void render(Graphics g) {        if (poly != null) {            poly.render(g);        }        super.render(g);    }    /**     * Set the polygon for the path.     */    public void setPoly(OMPoly p) {        poly = p;        if (poly.getRenderType() == OMGraphic.RENDERTYPE_LATLON) {            pathPoints = poly.getLatLonArray();            setLat(ProjMath.radToDeg(pathPoints[0]));            setLon(ProjMath.radToDeg(pathPoints[1]));            setStationary(false);        } else {            setStationary(true);        }    }    public OMPoly getPoly() {        return poly;    }    OMKeyBehavior platformBehavior;    public Behavior setViewingPlatformBehavior(TransformGroup ctg,                                               Projection projection,                                               float scaleFactor) {        if (DEBUG)            Debug.output("PilotPath setting viewing platform behavior");        cameraTransformGroup = ctg;        platformBehavior = new OMKeyBehavior(cameraTransformGroup, viewProjection, locateWorld(projection,                scaleFactor));        // Trying to look down a little, didn't work. Should have,        // though.        //      platformBehavior.doLookX(com.bbn.openmap.MoreMath.HALF_PI/2f);        return platformBehavior;    }    /**     */    public Vector3d locateWorld(Projection projection, float scaleFactor) {        // Set the view parameters.        this.viewProjection = projection;        translateTransform = new Transform3D();        this.scaleFactor = scaleFactor;        cameraTransformGroup.getTransform(translateTransform);        if (DEBUG)            Debug.output("PilotPath setting camera location, scaleFactor = "                    + this.scaleFactor);        Vector3d translate = new Vector3d();        Point pilotPoint = projection.forward(getLat(), getLon());        // scaleFactor of < 1 shrinks the object.(.5) is the scale.        // So, this lays out where the land is, in relation to the        // viewer. We should get the projection from the MapBean, and        // offset the transform to the middle of the map.        if (projection != null) {            double centerXOffset = pilotPoint.getX() * scaleFactor;            double centerYOffset = pilotPoint.getY() * scaleFactor;            if (DEBUG)                Debug.output("OM3DViewer with projection " + projection                        + ", setting center of scene to " + centerXOffset                        + ", " + centerYOffset);            translate.set(centerXOffset, (double) height, centerYOffset);            lastX = centerXOffset;            lastY = centerYOffset;        } else {            translate.set(0, (double) height, 0);        }        return translate;    }    /** Needed for J3D world. */    protected MapHandler mapHandler;    public void setMapHandler(MapHandler mh) {        mapHandler = mh;    }    public void launch3D() {        JFrame viewer = ControlledManager.getFrame("OpenMap 3D",                500,                500,                mapHandler,                (NavBehaviorProvider) this,                new javax.media.j3d.Background(.3f, .3f, .3f),                OM3DConstants.CONTENT_MASK_OMGRAPHICHANDLERLAYERS                        | OM3DConstants.CONTENT_MASK_OM3DGRAPHICHANDLERS);        viewer.show();    }    public final static String Launch3DCmd = "Launch3D";    /**     * Gets the gui controls associated with the Pilot. This default     * implementation returns null indicating that the Pilot has no     * gui controls.     *      * @return java.awt.Component or null     */    public java.awt.Component getGUI() {        JPanel panel = new JPanel(new GridLayout(0, 1));        // Only want to do this once...        if (movementButton == null) {            movementButton = new JCheckBox("Stationary", getStationary());            movementButton.addActionListener(this);            movementButton.setActionCommand(MoveCmd);        }        panel.add(movementButton);        JPanel heightPanel = new JPanel(new GridLayout(0, 3));        heightPanel.add(new JLabel("Object height: "));        if (heightField == null) {            heightField = new JTextField(Float.toString(height), 10);            heightField.setHorizontalAlignment(JTextField.RIGHT);            heightField.addActionListener(this);            heightField.addFocusListener(this);        }        heightPanel.add(heightField);        // There aren't any units to this yet - we need a good        // translation between meters and the j3d world elevations.        heightPanel.add(new JLabel(" "));        panel.add(heightPanel);        JButton launch3DButton = new JButton("Launch 3D");        launch3DButton.setActionCommand(Launch3DCmd);        launch3DButton.addActionListener(this);        panel.add(launch3DButton);        return panel;    }    public void actionPerformed(java.awt.event.ActionEvent ae) {        super.actionPerformed(ae);        String cmd = ae.getActionCommand();        if (cmd == Launch3DCmd) {            launch3D();        }    }}

⌨️ 快捷键说明

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