Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / textcanvas / CanvasCharacter.java
index b181226..a616dae 100644 (file)
@@ -12,6 +12,9 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPo
 
 import java.awt.*;
 
+import static eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon.drawPolygon;
+import static eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas.FONT_CHAR_HEIGHT;
+import static eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas.FONT_CHAR_WIDTH;
 import static java.lang.String.valueOf;
 
 /**
@@ -31,53 +34,55 @@ public class CanvasCharacter extends AbstractCoordinateShape {
      */
     private char value;
 
+    /**
+     * The foreground color of the character.
+     */
     private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
 
+    /**
+     * The background color of the character.
+     */
     private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
 
-    public CanvasCharacter(final Point3D point, final char character,
+    public CanvasCharacter(final Point3D centerLocation, final char character,
                            final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
                            final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
 
+        // There are 5 coordinates: center, upper left, upper right, lower right, lower left
         super(5);
-        coordinates[0].coordinate = point;
 
         value = character;
-
         this.foregroundColor = foregroundColor;
-
         this.backgroundColor = backgroundColor;
 
-        // set corner coordinates (for drawing background)
-        {
-            final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d;
-            final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d;
 
-            // upper left
-            coordinates[1].coordinate = point.clone().translateX(-widthHalf)
-                    .translateY(-heightHalf);
+        coordinates[0].coordinate = centerLocation;
+
+        final double halfWidth = FONT_CHAR_WIDTH / 2d;
+        final double halfHeight = FONT_CHAR_HEIGHT / 2d;
 
-            // upper right
-            coordinates[2].coordinate = point.clone().translateX(widthHalf)
-                    .translateY(-heightHalf);
+        // upper left
+        coordinates[1].coordinate = centerLocation.clone().translateX(-halfWidth)
+                .translateY(-halfHeight);
 
-            // lower right
-            coordinates[3].coordinate = point.clone().translateX(widthHalf)
-                    .translateY(heightHalf);
+        // upper right
+        coordinates[2].coordinate = centerLocation.clone().translateX(halfWidth)
+                .translateY(-halfHeight);
 
-            // lower left
-            coordinates[4].coordinate = point.clone().translateX(-widthHalf)
-                    .translateY(heightHalf);
+        // lower right
+        coordinates[3].coordinate = centerLocation.clone().translateX(halfWidth)
+                .translateY(halfHeight);
 
-        }
+        // lower left
+        coordinates[4].coordinate = centerLocation.clone().translateX(-halfWidth)
+                .translateY(halfHeight);
     }
 
     /**
      * Returns a font of the specified size.
      * <p>
-     *     If the font of the specified size is already cached, it will be
-     *     returned. Otherwise, a new font will be created, cached and returned.
-     *
+     * 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
@@ -108,6 +113,7 @@ public class CanvasCharacter extends AbstractCoordinateShape {
 
     /**
      * Returns color of the foreground.
+     *
      * @return the color
      */
     public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
@@ -116,6 +122,7 @@ public class CanvasCharacter extends AbstractCoordinateShape {
 
     /**
      * Sets color of the foreground.
+     *
      * @param foregroundColor the color
      */
     public void setForegroundColor(
@@ -123,25 +130,32 @@ public class CanvasCharacter extends AbstractCoordinateShape {
         this.foregroundColor = foregroundColor;
     }
 
+    /**
+     * Paints the character on the screen.
+     * @param renderingContext the rendering context
+     */
     @Override
     public void paint(final RenderingContext renderingContext) {
 
-        SolidPolygon.drawPolygon(renderingContext,
+        // Draw background rectangle first. It is composed of two triangles.
+        drawPolygon(renderingContext,
                 coordinates[1].onScreenCoordinate,
                 coordinates[2].onScreenCoordinate,
-                coordinates[3].onScreenCoordinate, mouseInteractionController,
+                coordinates[3].onScreenCoordinate,
+                mouseInteractionController,
                 backgroundColor);
 
-        SolidPolygon.drawPolygon(renderingContext,
+        drawPolygon(renderingContext,
                 coordinates[1].onScreenCoordinate,
                 coordinates[3].onScreenCoordinate,
-                coordinates[4].onScreenCoordinate, mouseInteractionController,
+                coordinates[4].onScreenCoordinate,
+                mouseInteractionController,
                 backgroundColor);
 
-        final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
+        final int desiredFontSize = (int) ((renderingContext.width * 4.5) / onScreenZ);
 
         // do not render too large characters
-        if (size >= MAX_FONT_SIZE)
+        if (desiredFontSize >= MAX_FONT_SIZE)
             return;
 
         final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
@@ -157,12 +171,13 @@ public class CanvasCharacter extends AbstractCoordinateShape {
         if (onScreenLocation.y > renderingContext.height)
             return;
 
-        renderingContext.graphics.setFont(getFont(size));
+        // draw the character
+        renderingContext.graphics.setFont(getFont(desiredFontSize));
         renderingContext.graphics.setColor(foregroundColor.toAwtColor());
         renderingContext.graphics.drawString(
                 valueOf(value),
-                (int) onScreenLocation.x - (int) (size / 3.2),
-                (int) onScreenLocation.y + (int) (size / 2.5));
+                (int) onScreenLocation.x - (int) (desiredFontSize / 3.2),
+                (int) onScreenLocation.y + (int) (desiredFontSize / 2.5));
 
     }