From 137348bb3e1b2b74f6d9d24954c40bca80b4ead7 Mon Sep 17 00:00:00 2001 From: Piotr Darosz Date: Tue, 4 Jun 2019 09:18:04 +0200 Subject: Text Inside Clamp box is not resizing Resizable text in Clamp boxes, better drawing space utilization. Change-Id: If6c79b22729730a8c243845b3288724f1c611b8a Issue-ID: CLAMP-402 Signed-off-by: Piotr Darosz --- .../org/onap/clamp/clds/util/drawing/AwtUtils.java | 30 ++++++++++++++++------ .../onap/clamp/clds/util/drawing/ImageBuilder.java | 3 ++- .../org/onap/clamp/clds/util/drawing/Painter.java | 4 +++ 3 files changed, 28 insertions(+), 9 deletions(-) (limited to 'src/main/java/org/onap') diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java b/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java index 1ece484b3..7a1f122ed 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java @@ -40,6 +40,7 @@ public class AwtUtils { private static final int FONT_STYLE = Font.PLAIN; private static final String FONT_FACE = "SansSerif"; private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f); + private static final int TEXT_PADDING = 5; private AwtUtils() { } @@ -51,7 +52,7 @@ public class AwtUtils { g2d.setColor(TRANSPARENT); g2d.fill(rect); g2d.setColor(oldColor); - addText(g2d, text, point.x + width / 2, point.y + height / 2); + addText(g2d, text, rect); } static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) { @@ -61,17 +62,30 @@ public class AwtUtils { g2d.fillPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3); } - private static void addText(Graphics2D g2d, String text, int abs, int ord) { + private static void addText(Graphics2D g2d, String text, Rectangle rect) { + int textBoundingBoxLimit = rect.width - 2* TEXT_PADDING; Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE); - g2d.setFont(font); - - FontMetrics fm1 = g2d.getFontMetrics(); - int w1 = fm1.stringWidth(text); - int x1 = abs - (w1 / 2); + font = scaleFontToFit(text, textBoundingBoxLimit, g2d, font); + Font oldFont = g2d.getFont(); g2d.setFont(font); g2d.setColor(Color.BLACK); - g2d.drawString(text, x1, ord); + FontMetrics fm1 = g2d.getFontMetrics(); + float x1 = rect.x + (float)(rect.width - fm1.stringWidth(text)) / 2; + float y1 = rect.y + (float)(rect.height - fm1.getHeight()) / 2 + fm1.getAscent(); + g2d.drawString(text, x1, y1); + + g2d.setFont(oldFont); + } + + private static Font scaleFontToFit(String text, int width, Graphics2D g2d, Font pFont) { + float fontSize = pFont.getSize(); + float fWidth = g2d.getFontMetrics(pFont).stringWidth(text); + if(fWidth <= width) { + return pFont; + } + fontSize = ((float)width / fWidth) * fontSize; + return pFont.deriveFont(fontSize); } } diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java b/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java index ce21c4cfd..5d37701fb 100644 --- a/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java @@ -36,6 +36,7 @@ public class ImageBuilder { public static final int POLICY_LINE_RATIO = 2; public static final int COLLECTOR_LINE_RATIO = 6; public static final float MS_LINE_TO_HEIGHT_RATIO = 0.75f; + public static final float ARROW_TO_BASELINE_RATIO = 0.75f; private Point currentPoint; private final int baseLength; @@ -68,7 +69,7 @@ public class ImageBuilder { ImageBuilder arrow() { String dataElementId = "Arrow-" + UUID.randomUUID().toString(); - Point to = new Point(currentPoint.x + baseLength, currentPoint.y); + Point to = new Point(currentPoint.x + (int)(baseLength*ARROW_TO_BASELINE_RATIO), currentPoint.y); AwtUtils.drawArrow(g2d, currentPoint, to, LINE_THICKNESS); documentBuilder.pushChangestoDocument(g2d, dataElementId); currentPoint = to; diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java b/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java index d88a17e86..ebb267f7b 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java @@ -43,6 +43,7 @@ public class Painter { private static final int THICK_LINE = 4; private static final double RECT_RATIO = 3.0 / 2.0; private static final int CIRCLE_RADIUS = 17; + private static final int MINIMUM_BASE_LENGTH = 120; /** * Constructor to create instance of Painter. @@ -60,6 +61,9 @@ public class Painter { int numOfRectangles = 2 + microServices.size(); int numOfArrows = numOfRectangles + 1; int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles); + if(baseLength < MINIMUM_BASE_LENGTH) { + baseLength = MINIMUM_BASE_LENGTH; + } int rectHeight = (int) (baseLength / RECT_RATIO); adjustGraphics2DProperties(); -- cgit 1.2.3-korg