2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * Modifications copyright (c) 2019 AT&T.
23 * ===================================================================
27 package org.onap.clamp.clds.util.drawing;
29 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics2D;
33 import java.awt.Point;
34 import java.awt.Rectangle;
36 public class AwtUtils {
37 private static final int ARROW_W = 4;
38 private static final int ARROW_H = 2;
39 private static final int FONT_SIZE = 12;
40 private static final int FONT_STYLE = Font.PLAIN;
41 private static final String FONT_FACE = "SansSerif";
42 private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f);
43 private static final int TEXT_PADDING = 5;
48 static void rectWithText(Graphics2D g2d, String text, Point point, int width, int height) {
49 Rectangle rect = new Rectangle(point.x, point.y, width, height);
51 Color oldColor = g2d.getColor();
52 g2d.setColor(TRANSPARENT);
54 g2d.setColor(oldColor);
55 addText(g2d, text, rect);
58 static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) {
59 int x2 = to.x - lineThickness;
60 g2d.drawLine(from.x, from.y, x2 - lineThickness, to.y);
61 g2d.drawPolygon(new int[] { x2 - ARROW_W, x2 - ARROW_W, x2 },
62 new int[] { to.y - ARROW_H, to.y + ARROW_H, to.y }, 3);
63 g2d.fillPolygon(new int[] { x2 - ARROW_W, x2 - ARROW_W, x2 },
64 new int[] { to.y - ARROW_H, to.y + ARROW_H, to.y }, 3);
67 private static void addText(Graphics2D g2d, String text, Rectangle rect) {
68 int textBoundingBoxLimit = rect.width - 2 * TEXT_PADDING;
69 Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
70 font = scaleFontToFit(text, textBoundingBoxLimit, g2d, font);
71 Font oldFont = g2d.getFont();
74 g2d.setColor(Color.BLACK);
75 FontMetrics fm1 = g2d.getFontMetrics();
76 float x1 = rect.x + (float) (rect.width - fm1.stringWidth(text)) / 2;
77 float y1 = rect.y + (float) (rect.height - fm1.getHeight()) / 2 + fm1.getAscent();
78 g2d.drawString(text, x1, y1);
83 private static Font scaleFontToFit(String text, int width, Graphics2D g2d, Font font) {
84 float fontSize = font.getSize();
85 float stringWidth = g2d.getFontMetrics(font).stringWidth(text);
86 if (stringWidth <= width) {
89 fontSize = (width / stringWidth) * fontSize;
90 return font.deriveFont(fontSize);