float yMin = (float)planBounds.getMinY() - MARGIN;
float xMax = convertXPixelToModel(getWidth());
float yMax = convertYPixelToModel(getHeight());
boolean leftToRightOriented = getComponentOrientation().isLeftToRight();
FontMetrics metrics = getFontMetrics(getFont());
int fontAscent = metrics.getAscent();
float tickSize = 5 / rulerScale;
float mainTickSize = (fontAscent + 6) / rulerScale;
NumberFormat format = NumberFormat.getIntegerInstance();
g2D.setColor(getForeground());
float lineWidth = 0.5f / rulerScale;
g2D.setStroke(new BasicStroke(lineWidth));
if (this.orientation == SwingConstants.HORIZONTAL) {
// Draw horizontal rule base
g2D.draw(new Line2D.Float(xMin, yMax - lineWidth, xMax, yMax - lineWidth));
// Draw small ticks
for (float x = (int) (xMin / gridSize) * gridSize; x < xMax; x += gridSize) {
g2D.draw(new Line2D.Float(x, yMax - tickSize, x, yMax));
}
} else {
// Draw vertical rule base
if (leftToRightOriented) {
g2D.draw(new Line2D.Float(xMax - lineWidth, yMin, xMax - lineWidth, yMax));
} else {
g2D.draw(new Line2D.Float(xMin + lineWidth, yMin, xMin + lineWidth, yMax));
}
// Draw small ticks
for (float y = (int) (yMin / gridSize) * gridSize; y < yMax; y += gridSize) {
if (leftToRightOriented) {
g2D.draw(new Line2D.Float(xMax - tickSize, y, xMax, y));
} else {
g2D.draw(new Line2D.Float(xMin, y, xMin + tickSize, y));
}
}
}
if (mainGridSize != gridSize) {
g2D.setStroke(new BasicStroke(1.5f / rulerScale,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
AffineTransform previousTransform = g2D.getTransform();
// Draw big ticks
if (this.orientation == SwingConstants.HORIZONTAL) {
for (float x = (int) (xMin / mainGridSize) * mainGridSize; x < xMax; x += mainGridSize) {
g2D.draw(new Line2D.Float(x, yMax - mainTickSize, x, yMax));
// Draw unit text
g2D.translate(x, yMax - mainTickSize);
g2D.scale(1 / rulerScale, 1 / rulerScale);
g2D.drawString(getFormattedTickText(format, x), 3, fontAscent - 1);
g2D.setTransform(previousTransform);
}
} else {
for (float y = (int) (yMin / mainGridSize) * mainGridSize; y < yMax; y += mainGridSize) {
String yText = getFormattedTickText(format, y);
if (leftToRightOriented) {
g2D.draw(new Line2D.Float(xMax - mainTickSize, y, xMax, y));
// Draw unit text with a vertical orientation
g2D.translate(xMax - mainTickSize, y);
g2D.scale(1 / rulerScale, 1 / rulerScale);
g2D.rotate(-Math.PI / 2);
g2D.drawString(yText, -metrics.stringWidth(yText) - 3, fontAscent - 1);
} else {
g2D.draw(new Line2D.Float(xMin, y, xMin + mainTickSize, y));
// Draw unit text with a vertical orientation
g2D.translate(xMin + mainTickSize, y);
g2D.scale(1 / rulerScale, 1 / rulerScale);