Changeset 16894 in josm


Ignore:
Timestamp:
2020-08-15T00:15:18+02:00 (7 months ago)
Author:
simon04
Message:

fix #4888 - SlippyMapBBoxChooser: show map scale

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MapScaler.java

    r12987 r16894  
    99import java.awt.Graphics;
    1010import java.awt.geom.Rectangle2D;
     11import java.util.function.DoubleSupplier;
     12import java.util.function.Supplier;
    1113
    1214import javax.accessibility.Accessible;
     
    2426public class MapScaler extends JComponent implements Helpful, Accessible {
    2527
    26     private final NavigatableComponent mv;
     28    private final DoubleSupplier getDist100Pixel;
     29    private final Supplier<Color> colorSupplier;
    2730
    2831    private static final int PADDING_LEFT = 5;
     
    3639     */
    3740    public MapScaler(NavigatableComponent mv) {
    38         this.mv = mv;
     41        this(() -> mv.getDist100Pixel(true), MapScaler::getColor);
     42    }
     43
     44    /**
     45     * Constructs a new {@code MapScaler}.
     46     * @param getDist100Pixel supplier for distance in meter that correspond to 100 px on screen
     47     * @param colorSupplier supplier for color
     48     */
     49    public MapScaler(DoubleSupplier getDist100Pixel, Supplier<Color> colorSupplier) {
     50        this.getDist100Pixel = getDist100Pixel;
     51        this.colorSupplier = colorSupplier;
    3952        setPreferredLineLength(100);
    4053        setOpaque(false);
     
    5164    @Override
    5265    public void paint(Graphics g) {
    53         g.setColor(getColor());
    54 
    55         double dist100Pixel = mv.getDist100Pixel(true);
     66        g.setColor(colorSupplier.get());
     67        double dist100Pixel = getDist100Pixel.getAsDouble();
    5668        TickMarks tickMarks = new TickMarks(dist100Pixel, getWidth() - PADDING_LEFT - PADDING_RIGHT);
    5769        tickMarks.paintTicks(g);
     
    8395        @Override
    8496        public Number getCurrentAccessibleValue() {
    85             return mv.getDist100Pixel();
     97            return getDist100Pixel.getAsDouble();
    8698        }
    8799
  • trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

    r16893 r16894  
    3535import org.openstreetmap.josm.data.preferences.StringProperty;
    3636import org.openstreetmap.josm.gui.MainApplication;
     37import org.openstreetmap.josm.gui.MapScaler;
     38import org.openstreetmap.josm.gui.NavigatableComponent;
    3739import org.openstreetmap.josm.gui.layer.ImageryLayer;
    3840import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
     
    119121        add(iSizeButton);
    120122
     123        MapScaler scaler = new MapScaler(this::getDist100Pixel, () -> Color.BLACK);
     124        add(scaler);
     125        springLayout.putConstraint(SpringLayout.SOUTH, scaler, 5, SpringLayout.SOUTH, this);
     126
    121127        String mapStyle = PROP_MAPSTYLE.get();
    122128        boolean foundSource = false;
     
    149155            LinkedHashMap::new
    150156        ));
     157    }
     158
     159    /**
     160     * Get the distance in meter that correspond to 100 px on screen.
     161     * @return the distance in meter that correspond to 100 px on screen
     162     * @see NavigatableComponent#getDist100Pixel
     163     */
     164    private double getDist100Pixel() {
     165        int w = getWidth() / 2;
     166        int h = getHeight() / 2;
     167        ICoordinate c1 = getPosition(w - 50, h);
     168        ICoordinate c2 = getPosition(w + 50, h);
     169        final LatLon ll1 = new LatLon(c1.getLat(), c1.getLon());
     170        final LatLon ll2 = new LatLon(c2.getLat(), c2.getLon());
     171        double gcd = ll1.greatCircleDistance(ll2);
     172        return gcd <= 0 ? 0.1 : gcd;
    151173    }
    152174
Note: See TracChangeset for help on using the changeset viewer.