📄 distancemousemode.java
字号:
// add the units infoLine = infoLine + unitInfo + ")"; // add the azimuth angle if need be if (showAngle) { infoLine = infoLine + ", angle (" + df.format(azimuth) + ")"; } return infoLine; } /** * Process a mouseEntered event. Record the mouse source object, a * map bean. * * @param e mouse event. */ public void mouseEntered(MouseEvent e) { // get the map bean if (e.getSource() instanceof MapBean) theMap = (MapBean) (e.getSource()); } /** * Process a mouseExited event. If a line is being drawn (and * mouse go off the map), it will be erased. The anchor point * rPoint1 is kept in case the mouse comes back on the screen. * Then, a new line will be drawn with the original mouse press * position. * * @param e mouse event. */ public void mouseExited(MouseEvent e) { if (e.getSource() instanceof MapBean) { // erase the old line first paintRubberband(rPoint1, rPoint2); // set the second point to null so that a new line will be // re-drawn if the mouse comes back, and the line will use // the old starting point. rPoint2 = null; } } /** * Draw a rubberband line between two points * * @param pt1 the anchor point. * @param pt2 the current (mouse) position. */ public void paintLine(LatLonPoint pt1, LatLonPoint pt2) { if (theMap != null) { paintLine(pt1, pt2, theMap.getGraphics()); } } /** * Draw a rubberband line between two points into the Graphics * object. * * @param pt1 the anchor point. * @param pt2 the current (mouse) position. * @param graphics a java.awt.Graphics object to render into. */ public void paintLine(LatLonPoint pt1, LatLonPoint pt2, Graphics graphics) { Graphics2D g = (Graphics2D) graphics; g.setXORMode(java.awt.Color.lightGray); g.setColor(java.awt.Color.darkGray); if (pt1 != null && pt2 != null) { // the line connecting the segments OMLine cLine = new OMLine(pt1.getLatitude(), pt1.getLongitude(), pt2.getLatitude(), pt2.getLongitude(), lineType); // get the map projection Projection proj = theMap.getProjection(); // prepare the line for rendering cLine.generate(proj); // render the line graphic cLine.render(g); } } /** * Draw a rubberband circle between two points * * @param pt1 the anchor point. * @param pt2 the current (mouse) position. */ public void paintCircle(LatLonPoint pt1, LatLonPoint pt2) { if (theMap != null) { paintCircle(pt1, pt2, theMap.getGraphics()); } } /** * Draw a rubberband circle between two points * * @param pt1 the anchor point. * @param pt2 the current (mouse) position. * @param graphics a java.awt.Graphics object to render into. */ public void paintCircle(LatLonPoint pt1, LatLonPoint pt2, Graphics graphics) { // do all this only if want to display the rubberband circle if (displayCircle) { Graphics2D g = (Graphics2D) graphics; g.setXORMode(java.awt.Color.lightGray); g.setColor(java.awt.Color.darkGray); if (pt1 != null && pt2 != null) { // first convert degrees to radians float radphi1 = ProjMath.degToRad(pt1.getLatitude()); float radlambda0 = ProjMath.degToRad(pt1.getLongitude()); float radphi = ProjMath.degToRad(pt2.getLatitude()); float radlambda = ProjMath.degToRad(pt2.getLongitude()); // calculate the circle radius double dRad = GreatCircle.spherical_distance(radphi1, radlambda0, radphi, radlambda); // convert into decimal degrees float rad = (float) ProjMath.radToDeg(dRad); // make the circle OMCircle circle = new OMCircle(pt1.getLatitude(), pt1.getLongitude(), rad); // get the map projection Projection proj = theMap.getProjection(); // prepare the circle for rendering circle.generate(proj); // render the circle graphic circle.render(g); } } // end if(displayCircle) } /** * Draw a rubberband line and circle between two points * * @param pt1 the anchor point. * @param pt2 the current (mouse) position. */ public void paintRubberband(LatLonPoint pt1, LatLonPoint pt2) { if (theMap != null) { paintRubberband(pt1, pt2, theMap.getGraphics()); } } /** * Draw a rubberband line and circle between two points * * @param pt1 the anchor point. * @param pt2 the current (mouse) position. * @param g a java.awt.Graphics object to render into. */ public void paintRubberband(LatLonPoint pt1, LatLonPoint pt2, Graphics g) { paintLine(pt1, pt2, g); paintCircle(pt1, pt2, g); } /** * Erase all line segments. */ public void eraseLines() { for (int i = 0; i < segments.size() - 1; i++) { paintLine((LatLonPoint) (segments.elementAt(i)), (LatLonPoint) (segments.elementAt(i + 1))); } } /** * Erase the current segment circle. */ public void eraseCircle() { paintCircle(rPoint1, rPoint2); } /** * Reset the segments and distances */ public void cleanUp() { // a quick way to clean the vector segments = new Vector(); // reset the total distance totalDistance = 0.0; distance = 0.0; } /** * Return the distance in the chosen unit between two points (in * decimal degrees). Based on spherical arc distance between two * points. See class GreatCircle.java * * @param phi1 latitude in decimal degrees of start point * @param lambda0 longitude in decimal degrees of start point * @param phi latitude in decimal degrees of end point * @param lambda longitude in decimal degrees of end point * @param units the unit of distance, DISTANCE_NM, DISTANCE_KM, * DISTANCE_MILE or all 3 types DISTANCE_ALL * @return double distance in chosen unit */ public double getGreatCircleDist(float phi1, float lambda0, float phi, float lambda, int units) { double dist = 0; // convert arguments to radians float radphi1 = ProjMath.degToRad(phi1); float radlambda0 = ProjMath.degToRad(lambda0); float radphi = ProjMath.degToRad(phi); float radlambda = ProjMath.degToRad(lambda); // get the spherical distance in radians between the two // points double distRad = (double) GreatCircle.spherical_distance(radphi1, radlambda0, radphi, radlambda); // in the chosen unit if (units == 0) dist = distRad * Planet.wgs84_earthEquatorialCircumferenceNMiles / MoreMath.TWO_PI; if (units == 1) dist = distRad * Planet.wgs84_earthEquatorialCircumferenceKM / MoreMath.TWO_PI; if (units == 2) dist = distRad * Planet.wgs84_earthEquatorialCircumferenceMiles / MoreMath.TWO_PI; return dist; } /** * Return the azimuth angle in decimal degrees from north. Based * on spherical_azimuth. See class GreatCircle.java * * @param phi1 latitude in decimal degrees of start point * @param lambda0 longitude in decimal degrees of start point * @param phi latitude in decimal degrees of end point * @param lambda longitude in decimal degrees of end point * @return float azimuth angle in degrees */ public float getSphericalAzimuth(float phi1, float lambda0, float phi, float lambda) { // convert arguments to radians float radphi1 = ProjMath.degToRad(phi1); float radlambda0 = ProjMath.degToRad(lambda0); float radphi = ProjMath.degToRad(phi); float radlambda = ProjMath.degToRad(lambda); // get the spherical azimuth in radians between the two points float az = GreatCircle.spherical_azimuth(radphi1, radlambda0, radphi, radlambda); return ProjMath.radToDeg(az); } /** * Set the map bean. * * @param aMap a map bean */ public void setMapBean(MapBean aMap) { theMap = aMap; } /** * Return the map bean. */ public MapBean getMapBean() { return theMap; } /** * Set the unit of distance to be displayed: Length.NM, Length.KM * or Length.MILE. If null, displays all of them. */ public void setUnit(Length units) { unit = units; } /** * Return the unit of distance being displayed: Length.NM, * Length.KM or Length.MILE. If null, displays all of them. */ public Length getUnit() { return unit; } /** * Switch the display of the azimuth angle on or off. * * @param onOff true to display the azimuth angle, false to turn * off */ public void showAzimuth(boolean onOff) { showAngle = onOff; } /** * Whether the display of the azimuth angle on or off. */ public boolean getShowAzimuth() { return showAngle; } /** * Set the line type to be drawn see also OMGraphic * * @param lype either LINETYPE_GREATCIRCLE, LINETYPE_RHUMB, * LINETYPE_STRAIGHT */ public void setLineType(int lype) { lineType = lype; } /** * Return the line type either LINETYPE_GREATCIRCLE, * LINETYPE_RHUMB, LINETYPE_STRAIGHT */ public int getLineType() { return lineType; } /** * Set the drawing of the rubberband circle on/off. * * @param onOff true or false */ public void showCircle(boolean onOff) { displayCircle = onOff; } /** * Get whether the drawing of the rubberband circle on/off. */ public boolean getShowCircle() { return displayCircle; } public void setRepaintToClean(boolean rtc) { repaintToClean = rtc; } public boolean getRepaintToClean() { return repaintToClean; } /** * PropertyConsumer interface method. */ public void setProperties(String prefix, Properties setList) { super.setProperties(prefix, setList); prefix = PropUtils.getScopedPropertyPrefix(prefix); String name = setList.getProperty(prefix + UnitProperty); if (name != null) { Length length = Length.get(name); if (length != null) { setUnit(length); } } showCircle(PropUtils.booleanFromProperties(setList, prefix + ShowCircleProperty, true)); showAzimuth(PropUtils.booleanFromProperties(setList, prefix + ShowAngleProperty, true)); setRepaintToClean(PropUtils.booleanFromProperties(setList, prefix + RepaintToCleanProperty, false)); } /** * PropertyConsumer interface method. */ public Properties getProperties(Properties getList) { if (getList == null) { getList = new Properties(); } String prefix = PropUtils.getScopedPropertyPrefix(this); getList.put(prefix + UnitProperty, unit.toString()); getList.put(prefix + ShowCircleProperty, new Boolean(getShowCircle()).toString()); getList.put(prefix + ShowAngleProperty, new Boolean(getShowAzimuth()).toString()); getList.put(prefix + RepaintToCleanProperty, new Boolean(getRepaintToClean()).toString()); return getList; } /** * PropertyConsumer interface method. */ public Properties getPropertyInfo(Properties list) { list = super.getPropertyInfo(list); list.put(UnitProperty, "Units to use for measurements, from Length.name possibilities."); list.put(ShowCircleProperty, "Flag to set whether the range circle is drawn at the end of the line (true/false)."); list.put(ShowAngleProperty, "Flag to note the azimuth angle of the line in the information line (true/false)."); list.put(RepaintToCleanProperty, "Flag to tell the map to repaint to clean up on a double click (true/false)."); return list; } /** * Called by the MapBean when it repaints, to let the MouseMode * know when to update itself on the map. PaintListener interface. */ public void listenerPaint(java.awt.Graphics g) { for (int i = 0; i < segments.size() - 1; i++) { paintLine((LatLonPoint) (segments.elementAt(i)), (LatLonPoint) (segments.elementAt(i + 1)), g); } paintRubberband(rPoint1, rPoint2, g); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -