Ignore:
Timestamp:
2013-03-22T22:18:26+01:00 (13 years ago)
Author:
zverik
Message:

ImageryID refactoring and javadocs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialogButton.java

    r29382 r29384  
    55import javax.swing.ImageIcon;
    66import javax.swing.JButton;
     7import org.openstreetmap.josm.Main;
     8import org.openstreetmap.josm.data.coor.EastNorth;
     9import org.openstreetmap.josm.data.coor.LatLon;
     10import org.openstreetmap.josm.data.projection.Projection;
    711import org.openstreetmap.josm.gui.layer.ImageryLayer;
    812import org.openstreetmap.josm.tools.ImageProvider;
    913
    1014/**
    11  * A button which shows offset information.
     15 * A button which shows offset information. Must be spectacular, since it's the only
     16 * non-JOptionPane GUI in the plugin.
    1217 *
    13  * @author zverik
     18 * @author Zverik
     19 * @license WTFPL
    1420 */
    1521public class OffsetDialogButton extends JButton {
     
    2026    private double direction;
    2127
     28    /**
     29     * Initialize the button with an offset. Calculated all relevant values.
     30     * @param offset An offset to display on the button.
     31     */
    2232    public OffsetDialogButton( ImageryOffsetBase offset ) {
    2333        super();
     
    4959    }
    5060
     61    /**
     62     * Returns the offset associated with this button.
     63     */
    5164    public ImageryOffsetBase getOffset() {
    5265        return offset;
     
    6982    }
    7083
     84    /**
     85     * Calculates length and direction for two points in the imagery offset object.
     86     * @see #getLengthAndDirection(iodb.ImageryOffset, double, double)
     87     */
    7188    private double[] getLengthAndDirection( ImageryOffset offset ) {
    7289        ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
    7390        double[] dxy = layer == null ? new double[] {0.0, 0.0} : new double[] {layer.getDx(), layer.getDy()};
    74         return ImageryOffsetTools.getLengthAndDirection((ImageryOffset)offset, dxy[0], dxy[1]);
     91        return getLengthAndDirection(offset, dxy[0], dxy[1]);
    7592    }
    7693
     94    /**
     95     * Calculates length and direction for two points in the imagery offset object
     96     * taking into account an existing imagery layer offset.
     97     *
     98     * @see #getLengthAndDirection(iodb.ImageryOffset)
     99     */
     100    public static double[] getLengthAndDirection( ImageryOffset offset, double dx, double dy ) {
     101        Projection proj = Main.getProjection();
     102        EastNorth pos = proj.latlon2eastNorth(offset.getPosition());
     103        LatLon correctedCenterLL = proj.eastNorth2latlon(pos.add(-dx, -dy));
     104        double length = correctedCenterLL.greatCircleDistance(offset.getImageryPos());
     105        double direction = length < 1e-2 ? 0.0 : correctedCenterLL.heading(offset.getImageryPos());
     106        // todo: north vs south. Meanwhile, let's fix this dirty:
     107//        direction = Math.PI - direction;
     108        if( direction < 0 )
     109            direction += Math.PI * 2;
     110        return new double[] {length, direction};
     111    }
     112
     113    /**
     114     * An offset icon. Displays a plain calibration icon for a geometry
     115     * and an arrow for an imagery offset.
     116     */
    77117    class OffsetIcon implements Icon {
    78118        private boolean isDeprecated;
     
    82122        private ImageIcon background;
    83123
     124        /**
     125         * Initialize the icon with an offset object. Calculates length and direction
     126         * of an arrow if they're needed.
     127         */
    84128        public OffsetIcon( ImageryOffsetBase offset ) {
    85129            isDeprecated = offset.isDeprecated();
     
    89133                ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
    90134                double[] dxy = layer == null ? new double[] {0.0, 0.0} : new double[] { layer.getDx(), layer.getDy() };
    91                 double[] ld = ImageryOffsetTools.getLengthAndDirection((ImageryOffset)offset, dxy[0], dxy[1]);
     135                double[] ld = getLengthAndDirection((ImageryOffset)offset, dxy[0], dxy[1]);
    92136                length = ld[0];
    93137                direction = ld[1];
     
    97141        }
    98142
     143        /**
     144         * Paints the base image and adds to it according to the offset.
     145         */
    99146        public void paintIcon( Component comp, Graphics g, int x, int y ) {
    100147            background.paintIcon(comp, g, x, y);
     
    110157                } else {
    111158                    // draw an arrow
    112                     double arrowLength = length < 5 ? getIconWidth() / 2 - 1 : getIconWidth() - 4;
     159                    double arrowLength = length < 10 ? getIconWidth() / 2 - 1 : getIconWidth() - 4;
    113160                    g2.setStroke(new BasicStroke(2));
    114161                    int dx = (int)Math.round(Math.sin(direction) * arrowLength / 2);
Note: See TracChangeset for help on using the changeset viewer.