From 62bbb2ff1758eac40837441bce6470944ccbd999 Mon Sep 17 00:00:00 2001 From: Miguel Carvalho Date: Mon, 11 Nov 2024 16:39:12 +0000 Subject: [PATCH 1/3] util/RenderStringUtils: Created util for string outline rendering. --- .../lsts/neptus/util/RenderStringUtils.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/java/pt/lsts/neptus/util/RenderStringUtils.java diff --git a/src/java/pt/lsts/neptus/util/RenderStringUtils.java b/src/java/pt/lsts/neptus/util/RenderStringUtils.java new file mode 100644 index 0000000000..41285486d4 --- /dev/null +++ b/src/java/pt/lsts/neptus/util/RenderStringUtils.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2004-2024 Universidade do Porto - Faculdade de Engenharia + * Laboratório de Sistemas e Tecnologia Subaquática (LSTS) + * All rights reserved. + * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal + * + * This file is part of Neptus, Command and Control Framework. + * + * Commercial Licence Usage + * Licencees holding valid commercial Neptus licences may use this file + * in accordance with the commercial licence agreement provided with the + * Software or, alternatively, in accordance with the terms contained in a + * written agreement between you and Universidade do Porto. For licensing + * terms, conditions, and further information contact lsts@fe.up.pt. + * + * Modified European Union Public Licence - EUPL v.1.1 Usage + * Alternatively, this file may be used under the terms of the Modified EUPL, + * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md + * included in the packaging of this file. You may not use this work + * except in compliance with the Licence. Unless required by applicable + * law or agreed to in writing, software distributed under the Licence is + * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF + * ANY KIND, either express or implied. See the Licence for the specific + * language governing permissions and limitations at + * https://github.com/LSTS/neptus/blob/develop/LICENSE.md + * and http://ec.europa.eu/idabc/eupl.html. + * + * For more information please see . + * + * Author: Miguel Carvalho + * 2024/11/04 + */ +package pt.lsts.neptus.util; + +import java.awt.*; +import java.awt.font.GlyphVector; + +public class RenderStringUtils { + + /** To avoid instantiation */ + private RenderStringUtils() { + } + + /** + * Renders an outline of string text. + * + * @param g + * @param font + * @param textColor + * @param outlineColor + * @param text + * @param x + * @param y + * @return + */ + public static void drawStringWOutline(Graphics2D g, Font font, Color textColor, Color outlineColor, String text, double x, double y) { + g.setFont(font); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + // Calculate dynamic outline thickness based on font size + float outlineThickness = font.getSize() * 0.25f; + + GlyphVector glyphVector = font.createGlyphVector(g.getFontRenderContext(), text); + Shape textShape = glyphVector.getOutline((int) (x), (int) (y)); + + // Draw the outline + g.setColor(outlineColor); + g.setStroke(new BasicStroke(outlineThickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // Thickness of the outline + g.draw(textShape); + + // Draw the fill + g.setColor(textColor); + g.fill(textShape); + } +} From 094d5b0475844c6db21e1b1ec1d366e07758b8e5 Mon Sep 17 00:00:00 2001 From: Miguel Carvalho Date: Tue, 12 Nov 2024 10:22:59 +0000 Subject: [PATCH 2/3] util/RenderStringUtils: Removed wild card import. --- src/java/pt/lsts/neptus/util/RenderStringUtils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/java/pt/lsts/neptus/util/RenderStringUtils.java b/src/java/pt/lsts/neptus/util/RenderStringUtils.java index 41285486d4..91e3f20235 100644 --- a/src/java/pt/lsts/neptus/util/RenderStringUtils.java +++ b/src/java/pt/lsts/neptus/util/RenderStringUtils.java @@ -32,7 +32,12 @@ */ package pt.lsts.neptus.util; -import java.awt.*; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.Shape; import java.awt.font.GlyphVector; public class RenderStringUtils { From b8ba7c0eacbafa040a599eb7348067ac2e3b6ce2 Mon Sep 17 00:00:00 2001 From: Miguel Carvalho Date: Tue, 12 Nov 2024 10:27:23 +0000 Subject: [PATCH 3/3] util/RenderStringUtils: Changed the Graphics2D object in which the string is drawn to a new object instead of the original. --- .../lsts/neptus/util/RenderStringUtils.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/java/pt/lsts/neptus/util/RenderStringUtils.java b/src/java/pt/lsts/neptus/util/RenderStringUtils.java index 91e3f20235..9a1c5fa4e2 100644 --- a/src/java/pt/lsts/neptus/util/RenderStringUtils.java +++ b/src/java/pt/lsts/neptus/util/RenderStringUtils.java @@ -59,23 +59,31 @@ private RenderStringUtils() { * @return */ public static void drawStringWOutline(Graphics2D g, Font font, Color textColor, Color outlineColor, String text, double x, double y) { - g.setFont(font); - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + Graphics2D gtemp = (Graphics2D) g.create(); + try { + gtemp.setFont(font); + gtemp.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + gtemp.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - // Calculate dynamic outline thickness based on font size - float outlineThickness = font.getSize() * 0.25f; + // Calculate dynamic outline thickness based on font size + float outlineThickness = font.getSize() * 0.25f; - GlyphVector glyphVector = font.createGlyphVector(g.getFontRenderContext(), text); - Shape textShape = glyphVector.getOutline((int) (x), (int) (y)); + GlyphVector glyphVector = font.createGlyphVector(gtemp.getFontRenderContext(), text); + Shape textShape = glyphVector.getOutline((int) (x), (int) (y)); - // Draw the outline - g.setColor(outlineColor); - g.setStroke(new BasicStroke(outlineThickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // Thickness of the outline - g.draw(textShape); + // Draw the outline + gtemp.setColor(outlineColor); + gtemp.setStroke(new BasicStroke(outlineThickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // Thickness of the outline + gtemp.draw(textShape); - // Draw the fill - g.setColor(textColor); - g.fill(textShape); + // Draw the fill + gtemp.setColor(textColor); + gtemp.fill(textShape); + } + catch (Exception e) { + e.printStackTrace(); + } finally { + gtemp.dispose(); + } } }