Merge changes from topic 'feature/new-csar-installer'
[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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.util.drawing;
25
26 import java.awt.Color;
27 import java.awt.Font;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics2D;
30 import java.awt.Point;
31 import java.awt.Rectangle;
32
33 public class AwtUtils {
34     private static final int ARROW_W = 4;
35     private static final int ARROW_H = 2;
36     private static final  int FONT_SIZE = 12;
37     private static final  int FONT_STYLE = Font.PLAIN;
38     private static final String FONT_FACE = "SansSerif";
39     private static final Color TRANSPARENT = new Color(0.0f, 0.0f,0.0f,0.0f);
40
41     static void rectWithText(Graphics2D g2d, String text, Point p, int w, int h) {
42         Rectangle rect = new Rectangle(p.x, p.y, w, h);
43         g2d.draw(rect);
44         Color oldColor = g2d.getColor();
45         g2d.setColor(TRANSPARENT);
46         g2d.fill(rect);
47         g2d.setColor(oldColor);
48         addText(g2d, text, p.x+w/2, p.y+h/2);
49     }
50
51     static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) {
52         int x2 = to.x - lineThickness;
53         g2d.drawLine(from.x, from.y, x2-lineThickness, to.y);
54         g2d.drawPolygon(new int[] {x2-ARROW_W, x2-ARROW_W, x2},new int[] {to.y- ARROW_H, to.y+ ARROW_H, to.y},3);
55         g2d.fillPolygon(new int[] {x2-ARROW_W, x2-ARROW_W, x2},new int[] {to.y- ARROW_H, to.y+ ARROW_H, to.y},3);
56     }
57
58     private static void addText(Graphics2D g2d, String text, int x, int y) {
59         Font f = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
60         g2d.setFont(f);
61
62         FontMetrics fm1 = g2d.getFontMetrics();
63         int w1 = fm1.stringWidth(text);
64         int x1 = x - (w1 / 2);
65
66         g2d.setFont(f);
67         g2d.setColor(Color.BLACK);
68         g2d.drawString(text, x1, y);
69     }
70
71 }