From 324c5f8de858634643f8dd201cfcd99faa23af17 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 23 May 2023 23:31:33 +0300 Subject: [PATCH] Improved code readability --- .../composite/textcanvas/CanvasCharacter.java | 50 ++++++++++++++++--- .../composite/textcanvas/TextCanvas.java | 1 - .../composite/textcanvas/package-info.java | 2 +- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java index b754faf..b181226 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java @@ -12,14 +12,26 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPo import java.awt.*; +import static java.lang.String.valueOf; + +/** + * Represents a single character on the text canvas. + */ public class CanvasCharacter extends AbstractCoordinateShape { private static final int MAX_FONT_SIZE = 500; + + /** + * Cached fonts. + */ private static final Font[] fonts = new Font[MAX_FONT_SIZE]; - private String value; + + /** + * The character to be rendered. + */ + private char value; private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor; - private Color foregroundAwtColor; private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor; @@ -30,10 +42,9 @@ public class CanvasCharacter extends AbstractCoordinateShape { super(5); coordinates[0].coordinate = point; - value = String.valueOf(character); + value = character; this.foregroundColor = foregroundColor; - foregroundAwtColor = foregroundColor.toAwtColor(); this.backgroundColor = backgroundColor; @@ -61,6 +72,16 @@ public class CanvasCharacter extends AbstractCoordinateShape { } } + /** + * Returns a font of the specified size. + *

+ * If the font of the specified size is already cached, it will be + * returned. Otherwise, a new font will be created, cached and returned. + * + * + * @param size the size of the font + * @return the font + */ public static Font getFont(final int size) { if (fonts[size] != null) return fonts[size]; @@ -70,23 +91,36 @@ public class CanvasCharacter extends AbstractCoordinateShape { return font; } + /** + * Returns color of the background. + */ public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() { return backgroundColor; } + /** + * Sets color of the background. + */ public void setBackgroundColor( final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) { this.backgroundColor = backgroundColor; } + /** + * Returns color of the foreground. + * @return the color + */ public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() { return foregroundColor; } + /** + * Sets color of the foreground. + * @param foregroundColor the color + */ public void setForegroundColor( final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) { this.foregroundColor = foregroundColor; - foregroundAwtColor = foregroundColor.toAwtColor(); } @Override @@ -124,16 +158,16 @@ public class CanvasCharacter extends AbstractCoordinateShape { return; renderingContext.graphics.setFont(getFont(size)); - renderingContext.graphics.setColor(foregroundAwtColor); + renderingContext.graphics.setColor(foregroundColor.toAwtColor()); renderingContext.graphics.drawString( - value, + valueOf(value), (int) onScreenLocation.x - (int) (size / 3.2), (int) onScreenLocation.y + (int) (size / 2.5)); } public void setValue(final char value) { - this.value = String.valueOf(value); + this.value = value; } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java index 8c3e49f..4ffef13 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java @@ -186,7 +186,6 @@ public class TextCanvas extends TexturedRectangle { new char[]{character,}, 0, 1, (column * FONT_CHAR_WIDTH_TEXTURE_PIXELS), (row * FONT_CHAR_HEIGHT_TEXTURE_PIXELS) + (int) (FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.23f)); - getTexture().resetResampledBitmapCache(); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/package-info.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/package-info.java index 3d5e87e..cb4f6be 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/package-info.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/package-info.java @@ -3,7 +3,7 @@ * This project is released under Creative Commons Zero (CC0) license. *

* - * Text canvas is a canvas that can be used to render text. + * Text canvas is a 2D canvas that can be used to render text. */ package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas; \ No newline at end of file -- 2.20.1