- @Override
- public Rectangle2D getBoundsInLocal() {
- return null;
- }
-
- private static final transient int MAX_DIGITS = 0;
- private static final transient double EPSILON = 0.01;
- private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 2);
- //private static final transient String[] SI_UNIT_LARGE_PREFIXES = { "m", "km" };
-
- private static final transient double[] SCALE_VALUES = { 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 50000000 };
-
- public static String formatValue(double value) {
- int magnitude = (int) Math.round(Math.log10(value));
- //System.out.println("magnitude: " + magnitude + ", " + value);
- int allowedDecimals = MAX_DIGITS;
- allowedDecimals -= Math.abs(magnitude);
- if (allowedDecimals < 0)
- allowedDecimals = 0;
-
- String valueStr = String.format(Locale.US, "%." + allowedDecimals + "f", value);
- if (allowedDecimals > 0) {
- for (int trunc = valueStr.length() - 1; trunc > 0; --trunc) {
- char ch = valueStr.charAt(trunc);
- if (ch == '.') {
- valueStr = valueStr.substring(0, trunc);
- break;
- }
- if (valueStr.charAt(trunc) != '0') {
- valueStr = valueStr.substring(0, trunc + 1);
- break;
+ // Prevent the scale from getting too long in the X-direction
+ if (meterPerPixel < 0.005)
+ return;
+
+ double scaleWidth = findScaleWidth(meterPerPixel);
+ double scaleMinX = scaleMaxX - scaleWidth;
+ double value = scaleWidth * meterPerPixel;
+ String formattedValue = formatValue(value);
+ Rectangle2D textBounds = fm.getStringBounds(formattedValue, g);
+ double addedTextWidth = textBounds.getWidth() + MapInfoConstants.TEXT_HORIZONTAL_MARGIN * 2;
+ scaleMinX -= addedTextWidth;
+ scaleMaxX -= addedTextWidth;
+ rect.setFrame(scaleMinX, scaleMinY, scaleWidth + addedTextWidth, scaleTotalHeight);
+
+// System.out.println("----");
+// System.out.println("scale: " + scaleX + ", " + scaleY);
+// System.out.println("scaleMaxX: " + scaleMaxX + ", offsetX: " + offsetX);
+// System.out.println("scaleMinY: " + scaleMinY + ", offsetY: " + offsetY);
+// System.out.println("meterPerPixel: " + meterPerPixel);
+
+ Composite oc = g.getComposite();
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
+ g.setStroke(MapInfoConstants.INFO_STROKE);
+
+ g.setColor(MapInfoConstants.TEXT_BG_COLOR);
+ g.fill(rect);
+
+ g.setColor(Color.BLACK);
+ g.drawString(formattedValue, (int) (scaleMinX + scaleWidth) + MapInfoConstants.TEXT_HORIZONTAL_MARGIN, textY);
+
+ double stepX = scaleWidth;
+
+ g.setColor(GRAY);
+ int yOffset = -SCALE_VERTICAL_MARGIN;
+ g.drawLine((int) scaleMinX, (int) scaleMaxY + yOffset + 1, (int) scaleMaxX, (int) scaleMaxY + yOffset + 1);
+ g.drawLine((int) scaleMinX, (int) scaleMaxY - majorTickHeight + yOffset, (int) scaleMinX, (int) scaleMaxY + yOffset);
+ g.drawLine((int) scaleMaxX, (int) scaleMaxY - majorTickHeight + yOffset, (int) scaleMaxX, (int) scaleMaxY + yOffset);
+ double x = scaleMinX;
+ if (stepX / 5 > 2) {
+ for (double x2 = x + stepX / 5; x2 < x + stepX; x2 += stepX / 5) {
+ if (x2 > 20) {
+ g.drawLine((int) x2, (int) scaleMaxY - minorTickHeight + yOffset, (int) x2, (int) scaleMaxY + yOffset);