Fixed generic Sonar issue in the clamp project
[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
44     private AwtUtils() {
45     }
46
47     static void rectWithText(Graphics2D g2d, String text, Point point, int width, int height) {
48         Rectangle rect = new Rectangle(point.x, point.y, width, height);
49         g2d.draw(rect);
50         Color oldColor = g2d.getColor();
51         g2d.setColor(TRANSPARENT);
52         g2d.fill(rect);
53         g2d.setColor(oldColor);
54         addText(g2d, text, point.x + width / 2, point.y + height / 2);
55     }
56
57     static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) {
58         int x2 = to.x - lineThickness;
59         g2d.drawLine(from.x, from.y, x2 - lineThickness, to.y);
60         g2d.drawPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
61         g2d.fillPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
62     }
63
64     private static void addText(Graphics2D g2d, String text, int abs, int ord) {
65         Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
66         g2d.setFont(font);
67
68         FontMetrics fm1 = g2d.getFontMetrics();
69         int w1 = fm1.stringWidth(text);
70         int x1 = abs - (w1 / 2);
71
72         g2d.setFont(font);
73         g2d.setColor(Color.BLACK);
74         g2d.drawString(text, x1, ord);
75     }
76
77 }