e9e589e09000447d1622745dd9a3d4090b3248c0
[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  * Modifications copyright (c) 2019 AT&T.
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.util.drawing;
26
27 import java.awt.Color;
28 import java.awt.Font;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics2D;
31 import java.awt.Point;
32 import java.awt.Rectangle;
33
34 public class AwtUtils {
35     private static final int ARROW_W = 4;
36     private static final int ARROW_H = 2;
37     private static final int FONT_SIZE = 12;
38     private static final int FONT_STYLE = Font.PLAIN;
39     private static final String FONT_FACE = "SansSerif";
40     private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f);
41
42     static void rectWithText(Graphics2D g2d, String text, Point point, int width, int height) {
43         Rectangle rect = new Rectangle(point.x, point.y, width, height);
44         g2d.draw(rect);
45         Color oldColor = g2d.getColor();
46         g2d.setColor(TRANSPARENT);
47         g2d.fill(rect);
48         g2d.setColor(oldColor);
49         addText(g2d, text, point.x + width / 2, point.y + height / 2);
50     }
51
52     static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) {
53         int x2 = to.x - lineThickness;
54         g2d.drawLine(from.x, from.y, x2 - lineThickness, to.y);
55         g2d.drawPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
56         g2d.fillPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3);
57     }
58
59     private static void addText(Graphics2D g2d, String text, int abs, int ord) {
60         Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
61         g2d.setFont(font);
62
63         FontMetrics fm1 = g2d.getFontMetrics();
64         int w1 = fm1.stringWidth(text);
65         int x1 = abs - (w1 / 2);
66
67         g2d.setFont(font);
68         g2d.setColor(Color.BLACK);
69         g2d.drawString(text, x1, ord);
70     }
71
72 }