Skip to content

Commit

Permalink
Document Image-, Shape- and Textrenderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Sep 5, 2024
1 parent 6ab46f7 commit 4143196
Show file tree
Hide file tree
Showing 4 changed files with 416 additions and 333 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import java.awt.geom.Point2D;

/**
* This static implementation renders an {@code Image} to a given {@code Graphics2D} object at the specified screen
* coordinates. This class be very useful when composing a GUI that contains images which are rendered at a certain
* location on the screen.
* This class provides static methods to render an {@code Image} to a given {@code Graphics2D} object at specified screen coordinates. It includes
* methods for rendering images with transformations such as rotation, scaling, and custom affine transformations. This class is final and cannot be
* instantiated.
*
* @see Image
* @see Graphics2D
Expand All @@ -19,6 +19,14 @@ private ImageRenderer() {
throw new UnsupportedOperationException();
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified coordinates.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param x The x-coordinate of the image.
* @param y The y-coordinate of the image.
*/
public static void render(final Graphics2D g, final Image image, final double x, final double y) {
if (image == null) {
return;
Expand All @@ -28,6 +36,13 @@ public static void render(final Graphics2D g, final Image image, final double x,
g.drawImage(image, t, null);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified location.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param renderLocation The location where the image will be drawn.
*/
public static void render(final Graphics2D g, final Image image, final Point2D renderLocation) {
render(g, image, renderLocation.getX(), renderLocation.getY());
}
Expand All @@ -47,7 +62,7 @@ public static void render(final Graphics2D g, final Image image, final Point2D r
* The angle by which the image will be rotated.
*/
public static void renderRotated(
final Graphics2D g, final Image image, final double x, final double y, final double angle) {
final Graphics2D g, final Image image, final double x, final double y, final double angle) {
if (image == null) {
return;
}
Expand All @@ -65,37 +80,81 @@ public static void renderRotated(
g.drawImage(image, t, null);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified location with rotation.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param renderLocation The location where the image will be drawn.
* @param angle The angle by which the image will be rotated.
*/
public static void renderRotated(
final Graphics2D g, final Image image, final Point2D renderLocation, final double angle) {
final Graphics2D g, final Image image, final Point2D renderLocation, final double angle) {
renderRotated(g, image, renderLocation.getX(), renderLocation.getY(), angle);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified coordinates with scaling.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param x The x-coordinate of the image.
* @param y The y-coordinate of the image.
* @param scale The scale factor for both width and height.
*/
public static void renderScaled(
final Graphics2D g, final Image image, final double x, final double y, final double scale) {
final Graphics2D g, final Image image, final double x, final double y, final double scale) {
renderScaled(g, image, x, y, scale, scale);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified location with scaling.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param location The location where the image will be drawn.
* @param scale The scale factor for both width and height.
*/
public static void renderScaled(
final Graphics2D g, final Image image, final Point2D location, final double scale) {
final Graphics2D g, final Image image, final Point2D location, final double scale) {
renderScaled(g, image, location.getX(), location.getY(), scale, scale);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified location with scaling.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param location The location where the image will be drawn.
* @param scaleX The scale factor for the width.
* @param scaleY The scale factor for the height.
*/
public static void renderScaled(
final Graphics2D g,
final Image image,
final Point2D location,
final double scaleX,
final double scaleY) {
final Graphics2D g,
final Image image,
final Point2D location,
final double scaleX,
final double scaleY) {
renderScaled(g, image, location.getX(), location.getY(), scaleX, scaleY);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified coordinates with scaling.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param x The x-coordinate of the image.
* @param y The y-coordinate of the image.
* @param scaleX The scale factor for the width.
* @param scaleY The scale factor for the height.
*/
public static void renderScaled(
final Graphics2D g,
final Image image,
final double x,
final double y,
final double scaleX,
final double scaleY) {
final Graphics2D g,
final Image image,
final double x,
final double y,
final double scaleX,
final double scaleY) {
if (image == null) {
return;
}
Expand All @@ -113,16 +172,33 @@ public static void renderScaled(
g.drawImage(image, t, null);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified location with a custom affine transformation.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param renderLocation The location where the image will be drawn.
* @param transform The affine transformation to be applied to the image.
*/
public static void renderTransformed(
final Graphics2D g,
final Image image,
final Point2D renderLocation,
AffineTransform transform) {
final Graphics2D g,
final Image image,
final Point2D renderLocation,
AffineTransform transform) {
renderTransformed(g, image, renderLocation.getX(), renderLocation.getY(), transform);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object at the specified coordinates with a custom affine transformation.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param x The x-coordinate of the image.
* @param y The y-coordinate of the image.
* @param transform The affine transformation to be applied to the image.
*/
public static void renderTransformed(
final Graphics2D g, final Image image, double x, double y, AffineTransform transform) {
final Graphics2D g, final Image image, double x, double y, AffineTransform transform) {
if (transform == null) {
render(g, image, x, y);
return;
Expand All @@ -135,8 +211,15 @@ public static void renderTransformed(
g.drawImage(image, t, null);
}

/**
* Renders the specified {@code Image} to the given {@code Graphics2D} object with a custom affine transformation.
*
* @param g The graphics object to draw on.
* @param image The image to be drawn.
* @param transform The affine transformation to be applied to the image.
*/
public static void renderTransformed(
final Graphics2D g, final Image image, AffineTransform transform) {
final Graphics2D g, final Image image, AffineTransform transform) {
if (transform == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void setBaseRenderScale(float scale) {
* @param text The text to be rendered
* @param x The x-coordinate of the text.
* @param y The y-coordinate of the text
* @param antialias Configure whether or not to render the text with antialiasing.
* @param antialias Configure whether to render the text with antialiasing.
*/
public void renderText(
final Graphics2D g, final String text, final double x, final double y, boolean antialias) {
Expand Down Expand Up @@ -168,7 +168,7 @@ public void renderText(final Graphics2D g, final String text, final double x, fi
* @param g The graphics object to render on.
* @param text The text to be rendered.
* @param location The location on the map.
* @param antialias Configure whether or not to render the text with antialiasing.
* @param antialias Configure whether to render the text with antialiasing.
*/
public void renderText(
final Graphics2D g, final String text, final Point2D location, boolean antialias) {
Expand Down Expand Up @@ -201,7 +201,7 @@ public void renderShape(final Graphics2D g, final Shape shape) {
*
* @param g The graphics object to render on.
* @param shape The shape to be rendered.
* @param antialiasing Configure whether or not to render the shape with antialiasing.
* @param antialiasing Configure whether to render the shape with antialiasing.
*/
public void renderShape(final Graphics2D g, final Shape shape, boolean antialiasing) {
renderShape(g, shape, antialiasing, 0);
Expand All @@ -212,7 +212,7 @@ public void renderShape(final Graphics2D g, final Shape shape, boolean antialias
*
* @param g The graphics object to render on.
* @param shape The shape to be rendered.
* @param antialiasing Configure whether or not to render the shape with antialiasing.
* @param antialiasing Configure whether to render the shape with antialiasing.
* @param angle The angle by which the shape will be rotated.
*/
public void renderShape(
Expand Down Expand Up @@ -255,7 +255,7 @@ public void renderOutline(final Graphics2D g, final Shape shape) {
*
* @param g The graphics object to render on.
* @param shape The shape to be rendered.
* @param antialiasing Configure whether or not to render the shape with antialiasing.
* @param antialiasing Configure whether to render the shape with antialiasing.
*/
public void renderOutline(final Graphics2D g, final Shape shape, boolean antialiasing) {
renderOutline(
Expand All @@ -280,7 +280,7 @@ public void renderOutline(final Graphics2D g, final Shape shape, final Stroke st
* @param g The graphics object to render on.
* @param shape The shape to be rendered.
* @param stroke The stroke that is used to render the shape.
* @param antialiasing Configure whether or not to render the shape with antialiasing.
* @param antialiasing Configure whether to render the shape with antialiasing.
* @see Stroke
*/
public void renderOutline(
Expand All @@ -294,7 +294,7 @@ public void renderOutline(
* @param g The graphics object to render on.
* @param shape The shape to be rendered.
* @param stroke The stroke that is used to render the shape.
* @param antialiasing Configure whether or not to render the shape with antialiasing.
* @param antialiasing Configure whether to render the shape with antialiasing.
* @param angle The angle by which the shape will be rotated.
* @see Stroke
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,72 @@ private ShapeRenderer() {
throw new UnsupportedOperationException();
}

/**
* Renders the specified {@code Shape} to the given {@code Graphics2D} object.
*
* @param g The graphics object to draw on.
* @param shape The shape to be drawn.
*/
public static void render(final Graphics2D g, final Shape shape) {
if (shape == null) {
return;
}
g.fill(shape);
}

/**
* Renders the specified {@code Shape} to the given {@code Graphics2D} object at the specified coordinates.
*
* @param g The graphics object to draw on.
* @param shape The shape to be drawn.
* @param x The x-coordinate of the shape.
* @param y The y-coordinate of the shape.
*/
public static void render(final Graphics2D g, final Shape shape, double x, double y) {
g.translate(x, y);
render(g, shape);
g.translate(-x, -y);
}

/**
* Renders the specified {@code Shape} to the given {@code Graphics2D} object at the specified location.
*
* @param g The graphics object to draw on.
* @param shape The shape to be drawn.
* @param location The location where the shape will be drawn.
*/
public static void render(final Graphics2D g, final Shape shape, Point2D location) {
render(g, shape, location.getX(), location.getY());
}

/**
* Renders the outline of the specified {@code Shape} to the given {@code Graphics2D} object.
*
* @param g The graphics object to draw on.
* @param shape The shape whose outline is to be drawn.
*/
public static void renderOutline(final Graphics2D g, final Shape shape) {
renderOutline(g, shape, DEFAULT_STROKE);
}

/**
* Renders the outline of the specified {@code Shape} to the given {@code Graphics2D} object with the specified stroke width.
*
* @param g The graphics object to draw on.
* @param shape The shape whose outline is to be drawn.
* @param stroke The stroke width for the outline.
*/
public static void renderOutline(final Graphics2D g, final Shape shape, final float stroke) {
renderOutline(g, shape, new BasicStroke(stroke));
}

/**
* Renders the outline of the specified {@code Shape} to the given {@code Graphics2D} object with the specified stroke.
*
* @param g The graphics object to draw on.
* @param shape The shape whose outline is to be drawn.
* @param stroke The stroke for the outline.
*/
public static void renderOutline(final Graphics2D g, final Shape shape, final Stroke stroke) {
if (shape == null) {
return;
Expand All @@ -49,24 +90,54 @@ public static void renderOutline(final Graphics2D g, final Shape shape, final St
g.setStroke(oldStroke);
}

/**
* Renders the specified {@code Shape} to the given {@code Graphics2D} object with a custom affine transformation.
*
* @param g The graphics object to draw on.
* @param shape The shape to be drawn.
* @param transform The affine transformation to be applied to the shape.
*/
public static void renderTransformed(
final Graphics2D g, final Shape shape, AffineTransform transform) {
final Graphics2D g, final Shape shape, AffineTransform transform) {

render(g, transform.createTransformedShape(shape));
}

/**
* Renders the outline of the specified {@code Shape} to the given {@code Graphics2D} object with a custom affine transformation.
*
* @param g The graphics object to draw on.
* @param shape The shape whose outline is to be drawn.
* @param transform The affine transformation to be applied to the shape.
*/
public static void renderOutlineTransformed(
final Graphics2D g, final Shape shape, AffineTransform transform) {
final Graphics2D g, final Shape shape, AffineTransform transform) {
renderOutlineTransformed(g, shape, transform, DEFAULT_STROKE);
}

/**
* Renders the outline of the specified {@code Shape} to the given {@code Graphics2D} object with a custom affine transformation and stroke width.
*
* @param g The graphics object to draw on.
* @param shape The shape whose outline is to be drawn.
* @param transform The affine transformation to be applied to the shape.
* @param stroke The stroke width for the outline.
*/
public static void renderOutlineTransformed(
final Graphics2D g, final Shape shape, AffineTransform transform, final float stroke) {
final Graphics2D g, final Shape shape, AffineTransform transform, final float stroke) {
renderOutlineTransformed(g, shape, transform, new BasicStroke(stroke));
}

/**
* Renders the outline of the specified {@code Shape} to the given {@code Graphics2D} object with a custom affine transformation and stroke.
*
* @param g The graphics object to draw on.
* @param shape The shape whose outline is to be drawn.
* @param transform The affine transformation to be applied to the shape.
* @param stroke The stroke for the outline.
*/
public static void renderOutlineTransformed(
final Graphics2D g, final Shape shape, AffineTransform transform, final Stroke stroke) {
final Graphics2D g, final Shape shape, AffineTransform transform, final Stroke stroke) {
if (transform == null) {
renderOutline(g, shape, stroke);
return;
Expand Down
Loading

0 comments on commit 4143196

Please sign in to comment.