Text Inside Clamp box is not resizing
[clamp.git] / src / main / java / org / onap / clamp / clds / util / drawing / AwtUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights
6  *                             reserved.
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
24  *
25  */
26
27 package org.onap.clamp.clds.util.drawing;
28
29 import java.awt.Color;
30 import java.awt.Font;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics2D;
33 import java.awt.Point;
34 import java.awt.Rectangle;
35
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;
44
45     private AwtUtils() {
46     }
47
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);
50         g2d.draw(rect);
51         Color oldColor = g2d.getColor();
52         g2d.setColor(TRANSPARENT);
53         g2d.fill(rect);
54         g2d.setColor(oldColor);
55         addText(g2d, text, rect);
56     }
57
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}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
62         g2d.fillPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
63     }
64
65     private static void addText(Graphics2D g2d, String text, Rectangle rect) {
66         int textBoundingBoxLimit = rect.width - 2* TEXT_PADDING;
67         Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
68         font = scaleFontToFit(text, textBoundingBoxLimit, g2d, font);
69         Font oldFont = g2d.getFont();
70
71         g2d.setFont(font);
72         g2d.setColor(Color.BLACK);
73         FontMetrics fm1 = g2d.getFontMetrics();
74         float x1 = rect.x + (float)(rect.width - fm1.stringWidth(text)) / 2;
75         float y1 = rect.y + (float)(rect.height - fm1.getHeight()) / 2 + fm1.getAscent();
76         g2d.drawString(text, x1, y1);
77
78         g2d.setFont(oldFont);
79     }
80
81     private static Font scaleFontToFit(String text, int width, Graphics2D g2d, Font pFont) {
82         float fontSize = pFont.getSize();
83         float fWidth = g2d.getFontMetrics(pFont).stringWidth(text);
84         if(fWidth <= width) {
85             return pFont;
86         }
87         fontSize = ((float)width / fWidth) * fontSize;
88         return pFont.deriveFont(fontSize);
89     }
90
91 }