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;
/**
*/
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
/**
* Returns color of the foreground.
+ *
* @return the color
*/
public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
/**
* Sets color of the foreground.
+ *
* @param foregroundColor the color
*/
public void setForegroundColor(
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;
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));
}