]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/ScaleBar.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / gui / map / ScaleBar.java
1 package tim.prune.gui.map;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Graphics;
6 import javax.swing.JPanel;
7
8 import tim.prune.I18nManager;
9 import tim.prune.config.ColourScheme;
10 import tim.prune.config.Config;
11 import tim.prune.data.Unit;
12
13 /**
14  * Class to show a scale bar on the main map of GpsPrune
15  */
16 public class ScaleBar extends JPanel
17 {
18         /** zoom level */
19         private int _zoomLevel = -1;
20         /** y position */
21         private double _yPos = 0.0;
22
23         // Dimensions
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;
32
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};
41
42
43         /**
44          * Constructor
45          */
46         public ScaleBar()
47         {
48                 super();
49                 setOpaque(false);
50                 setPreferredSize(new Dimension(100, 20));
51         }
52
53         /**
54          * Paint method to override display
55          * @param inG graphics object
56          */
57         public void paint(Graphics inG)
58         {
59                 super.paint(inG);
60                 if (_zoomLevel > -1)
61                 {
62                         try {
63                                 final double distScaleFactor = Config.getUnitSet().getDistanceUnit().getMultFactorFromStd();
64                                 double drightSide = LEFT_OFFSET + _metricPixels[_zoomLevel] / 1000.0 / distScaleFactor;
65                                 int scale = _scales[_zoomLevel];
66
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)
74                                 {
75                                         rightSide /= 2;
76                                         scale /= 2;
77                                         // Abort if scale is now less than 1 unit (shouldn't ever be)
78                                         if (scale < 1) {return;}
79                                 }
80                                 // Abort if scale is negative (around poles)
81                                 if (rightSide < 1) {return;}
82
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);
95                                 // horizontal
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);
99                                 // 0 tick
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);
102                                 // end tick
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);
105                                 // text
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);
114                         }
115                         catch (ArrayIndexOutOfBoundsException ai) {}
116                 }
117         }
118
119         /**
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
124          */
125         private static String getScaleText(int inScale, Unit inDistUnit)
126         {
127                 if (inScale > 0) {
128                         // Positive scale means km or miles
129                         return "" + inScale     + " " +
130                                 I18nManager.getText(inDistUnit.getShortnameKey());
131                 }
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?
135         }
136
137         /**
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
141          */
142         public void updateScale(int inZoom, double inYPos)
143         {
144                 _zoomLevel = inZoom;
145                 _yPos = inYPos;
146         }
147 }