1 package tim.prune.gui.map;
4 import java.awt.Dimension;
5 import java.awt.Graphics;
6 import javax.swing.JPanel;
8 import tim.prune.I18nManager;
9 import tim.prune.config.ColourScheme;
10 import tim.prune.config.Config;
11 import tim.prune.data.Unit;
14 * Class to show a scale bar on the main map of GpsPrune
16 public class ScaleBar extends JPanel
19 private int _zoomLevel = -1;
21 private double _yPos = 0.0;
24 /** Offset from left side in pixels */
25 private static final int LEFT_OFFSET = 20;
26 /** Offset from top in pixels */
27 private static final int Y_OFFSET = 10;
28 /** Tick height in pixels */
29 private static final int TICK_HEIGHT = 5;
30 /** Margin between bar and end text in pixels */
31 private static final int MARGIN_WIDTH = 8;
33 /** scales for each zoom level */
34 private static final int[] _scales = {10000, 5000, 2000, 2000, 1000, 500, 200, 100,
35 50, 20, 10, 5, 2, 2, 1,
36 -2, -5, -10, -20, -50, -100, -200};
37 /** pixel counts for each zoom level (metric) */
38 private static final double[] _metricPixels = {64, 64, 51, 102, 102, 102, 81, 81,
39 81, 65, 65, 65, 52, 105, 105,
40 105, 83, 83, 83, 67, 67, 67};
50 setPreferredSize(new Dimension(100, 20));
54 * Paint method to override display
55 * @param inG graphics object
57 public void paint(Graphics inG)
63 final double distScaleFactor = Config.getUnitSet().getDistanceUnit().getMultFactorFromStd();
64 double drightSide = LEFT_OFFSET + _metricPixels[_zoomLevel] / 1000.0 / distScaleFactor;
65 int scale = _scales[_zoomLevel];
67 // work out cos(latitude) from y position, and apply to scale
68 final double angle = Math.PI * (1 - 2*_yPos);
69 final double lat = Math.atan(Math.sinh(angle));
70 final double cosLat = Math.cos(lat);
71 int rightSide = (int) (drightSide / cosLat);
72 // Adjust if scale is too large
73 while (rightSide > 300)
77 // Abort if scale is now less than 1 unit (shouldn't ever be)
78 if (scale < 1) {return;}
80 // Abort if scale is negative (around poles)
81 if (rightSide < 1) {return;}
83 // Determine colours to use
84 Color barColour = Config.getColourScheme().getColour(ColourScheme.IDX_TEXT);
85 Color blankColour = new Color(255-barColour.getRed(), 255-barColour.getGreen(), 255-barColour.getBlue());
86 // Should this blank colour be set to saturation zero?
87 // Draw blank bars behind
88 inG.setColor(blankColour);
89 inG.drawLine(LEFT_OFFSET, Y_OFFSET-1, rightSide+2, Y_OFFSET-1);
90 inG.drawLine(LEFT_OFFSET, Y_OFFSET+2, rightSide+2, Y_OFFSET+2);
91 inG.drawLine(LEFT_OFFSET-1, Y_OFFSET+2, LEFT_OFFSET-1, Y_OFFSET-TICK_HEIGHT);
92 inG.drawLine(LEFT_OFFSET+2, Y_OFFSET+2, LEFT_OFFSET+2, Y_OFFSET-TICK_HEIGHT);
93 inG.drawLine(rightSide-1, Y_OFFSET+2, rightSide-1, Y_OFFSET-TICK_HEIGHT);
94 inG.drawLine(rightSide+2, Y_OFFSET+2, rightSide+2, Y_OFFSET-TICK_HEIGHT);
96 inG.setColor(barColour);
97 inG.drawLine(LEFT_OFFSET, Y_OFFSET, rightSide, Y_OFFSET);
98 inG.drawLine(LEFT_OFFSET, Y_OFFSET+1, rightSide, Y_OFFSET+1);
100 inG.drawLine(LEFT_OFFSET, Y_OFFSET, LEFT_OFFSET, Y_OFFSET-TICK_HEIGHT);
101 inG.drawLine(LEFT_OFFSET+1, Y_OFFSET, LEFT_OFFSET+1, Y_OFFSET-TICK_HEIGHT);
103 inG.drawLine(rightSide, Y_OFFSET+1, rightSide, Y_OFFSET-TICK_HEIGHT);
104 inG.drawLine(rightSide+1, Y_OFFSET+1, rightSide+1, Y_OFFSET-TICK_HEIGHT);
106 String text = getScaleText(scale, Config.getUnitSet().getDistanceUnit());
107 inG.setColor(blankColour);
108 inG.drawString(text, rightSide+MARGIN_WIDTH-1, Y_OFFSET);
109 inG.drawString(text, rightSide+MARGIN_WIDTH+1, Y_OFFSET);
110 inG.drawString(text, rightSide+MARGIN_WIDTH, Y_OFFSET-1);
111 inG.drawString(text, rightSide+MARGIN_WIDTH, Y_OFFSET+1);
112 inG.setColor(barColour);
113 inG.drawString(text, rightSide+MARGIN_WIDTH, Y_OFFSET);
115 catch (ArrayIndexOutOfBoundsException ai) {}
120 * Get the scale text for the given scale
121 * @param inScale scale number
122 * @param inDistUnit distance unit
123 * @return scale text as string
125 private static String getScaleText(int inScale, Unit inDistUnit)
128 // Positive scale means km or miles
129 return "" + inScale + " " +
130 I18nManager.getText(inDistUnit.getShortnameKey());
132 // negative scale means a fraction
133 return "" + (-1.0 / inScale) + " " + I18nManager.getText(inDistUnit.getShortnameKey());
134 // might be nice to say 100m instead of 0.1km, 275ft instead of 0.2miles, etc - need to be done by Unit itself?
138 * Update the scale level
139 * @param inZoom new zoom level
140 * @param inYPos y position, where 0 is north pole, 1 is south pole
142 public void updateScale(int inZoom, double inYPos)