Fix legend boundary box floating point issue.

refs #48
This commit is contained in:
Fabian Becker 2015-12-09 15:32:27 +01:00
parent f0716a3576
commit 7457b33a5d

View File

@ -153,33 +153,36 @@ public class GraphPointSetLegend {
private void paintIn(Graphics g, int x, int y, int maxX) { private void paintIn(Graphics g, int x, int y, int maxX) {
FontMetrics fm = g.getFontMetrics(); FontMetrics fm = g.getFontMetrics();
double fontHeightSum = 0.0; double fontHeightSum = 0.0;
int yOffs = 5 + y + fm.getHeight(); // Padding is used for in-box padding and out-box positioning
int xOffs;
int minX = Integer.MAX_VALUE;
int padding = 5; int padding = 5;
// yOffs used as bottom-left position for text line
double yOffs = padding + y + fm.getHeight();
int minX = Integer.MAX_VALUE;
Color origCol = g.getColor(); Color origCol = g.getColor();
// Draw bounding box // Draw bounding box
for (Pair<String, Color> legendEntry : legendEntries) { for (Pair<String, Color> legendEntry : legendEntries) {
Rectangle2D stringBounds = fm.getStringBounds(legendEntry.head, g); Rectangle2D stringBounds = fm.getStringBounds(legendEntry.head, g);
minX = Math.min(minX, (int) (maxX - stringBounds.getWidth() - 15)); minX = Math.min(minX, (int) (maxX - stringBounds.getWidth() - 20));
fontHeightSum += stringBounds.getHeight(); // Compute the combined height + padding of each line
fontHeightSum += stringBounds.getHeight() + padding;
} }
int boxHeight = (int)(fontHeightSum + padding * legendEntries.size()); //
int boxHeight = (int)(fontHeightSum + padding);
int boxWidth = maxX - minX + 20 - padding;
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.fillRect(minX - 20, padding + y, maxX, boxHeight); g.fillRect(minX - 20, padding + y, boxWidth, boxHeight);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawRect(minX - 20, padding + y, maxX, boxHeight); g.drawRect(minX - 20, padding + y, boxWidth, boxHeight);
// avoid that an entry with identical label and color occurs multiple // avoid that an entry with identical label and color occurs multiple
// times. // times.
for (Pair<String, Color> legendEntry : legendEntries) { for (Pair<String, Color> legendEntry : legendEntries) {
g.setColor(legendEntry.tail); g.setColor(legendEntry.tail);
Rectangle2D stringBounds = fm.getStringBounds(legendEntry.head, g); Rectangle2D stringBounds = fm.getStringBounds(legendEntry.head, g);
xOffs = (int) (maxX - stringBounds.getWidth() - 10); g.drawString(legendEntry.head, minX + 5, (int)yOffs);
g.drawString(legendEntry.head, xOffs, yOffs); g.drawLine(minX - 15, (int)(yOffs - stringBounds.getHeight()/2) + 1, minX, (int)(yOffs - stringBounds.getHeight()/2) + 1);
g.drawLine(xOffs - 20, yOffs - (int)(stringBounds.getHeight()/2) + 1, xOffs - padding, yOffs - (int)(stringBounds.getHeight()/2) + 1);
yOffs += (padding + stringBounds.getHeight()); yOffs += (padding + stringBounds.getHeight());
} }
g.setColor(origCol); g.setColor(origCol);