- Font rulerFont = new Font("Tahoma", Font.PLAIN, 9);
-
- //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setStroke(new BasicStroke(1));
- g.setColor(new Color(0.9f, 0.9f, 0.9f, 0.75f));
-
- Rectangle2D bounds = g.getClipBounds();
- if(bounds == null) return; // FIXME
-
- double previousText = -100;
-
- double minY = bounds.getMaxY() - 30;
-
- double scaleRight = bounds.getMaxX() - 30;
-
- double meterPerPixel = getMeterPerPixel(scaleRight - offsetX, minY - offsetY, scaleX, scaleY);
-
- double pixels = 0;
- double value = 0;
- for (int i = 0; i < SCALE_VALUES.length; i++) {
- value = SCALE_VALUES[i];
- pixels = value / meterPerPixel;
- if (pixels > 100) {
- break;
- }
- }
-
-
- double newScaleLeft = scaleRight - pixels;
+ Rectangle2D controlBounds = g.getClipBounds();
+ if (controlBounds == null)
+ return; // FIXME
+
+ Font font = MapInfoConstants.getInfoFont();
+ g.setFont(font);
+ FontMetrics fm = g.getFontMetrics();
+
+ int majorTickHeight = DPIUtil.upscale(SCALE_MAJOR_TICK_HEIGHT);
+ int minorTickHeight = DPIUtil.upscale(SCALE_MINOR_TICK_HEIGHT);
+ int microTickHeight = DPIUtil.upscale(SCALE_MICRO_TICK_HEIGHT);
+ double yOffsetFromBottom = getMapInfoNextY(g);
+
+ double scaleTotalHeight = Math.max(majorTickHeight + SCALE_VERTICAL_MARGIN * 2 , font.getSize() + MapInfoConstants.TEXT_VERTICAL_MARGIN * 2);
+ double scaleMaxX = controlBounds.getMaxX() - SCALE_RIGHT_MARGIN;
+ double scaleMaxY = controlBounds.getMaxY() - yOffsetFromBottom;
+ double scaleMinY = scaleMaxY - scaleTotalHeight;
+ int textY = (int) scaleMinY + MapInfoConstants.TEXT_VERTICAL_MARGIN + fm.getMaxAscent();
+ double meterPerPixel = getMeterPerPixel(scaleMaxX - offsetX, scaleMinY - offsetY, scaleX, scaleY);
+
+ // 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();