2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.util.drawing;
26 import java.awt.Color;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics2D;
30 import java.awt.Point;
31 import java.awt.Rectangle;
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);
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);
44 Color oldColor = g2d.getColor();
45 g2d.setColor(TRANSPARENT);
47 g2d.setColor(oldColor);
48 addText(g2d, text, p.x+w/2, p.y+h/2);
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);
58 private static void addText(Graphics2D g2d, String text, int x, int y) {
59 Font f = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE);
62 FontMetrics fm1 = g2d.getFontMetrics();
63 int w1 = fm1.stringWidth(text);
64 int x1 = x - (w1 / 2);
67 g2d.setColor(Color.BLACK);
68 g2d.drawString(text, x1, y);