Skip to content

Commit

Permalink
(#10, #32) added Polygon2DBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jul 4, 2021
1 parent a28a4b5 commit c535bba
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tech.fastj.graphics.game;

import tech.fastj.math.Pointf;
import tech.fastj.graphics.RenderStyle;
import tech.fastj.graphics.Transform2D;

import java.awt.Color;
import java.awt.Paint;
import java.awt.Stroke;
import java.util.Objects;

public class Polygon2DBuilder {

private final Pointf[] points;
private final boolean shouldRender;
private final RenderStyle renderStyle;

private Paint fillPaint = Polygon2D.DefaultPaint;
private Stroke outlineStroke = Polygon2D.DefaultOutlineStroke;
private Color outlineColor = Polygon2D.DefaultOutlineColor;

private Pointf translation = Transform2D.DefaultTranslation.copy();
private float rotation = Transform2D.DefaultRotation;
private Pointf scale = Transform2D.DefaultScale.copy();

Polygon2DBuilder(Pointf[] points, RenderStyle renderStyle, boolean shouldRender) {
this.points = Objects.requireNonNull(points, "The array of points must not be null.");
this.renderStyle = Objects.requireNonNull(renderStyle, "The render style must not be null.");
this.shouldRender = shouldRender;
}

public Polygon2DBuilder withFill(Paint fillPaint) {
this.fillPaint = Objects.requireNonNull(fillPaint, "The fill must not be null.");
return this;
}

public Polygon2DBuilder withOutline(Stroke outlineStroke, Color outlineColor) {
this.outlineStroke = Objects.requireNonNull(outlineStroke, "The outline stroke must not be null.");
this.outlineColor = Objects.requireNonNull(outlineColor, "The outline color must not be null.");
return this;
}

public Polygon2DBuilder withTransform(Pointf translation, float rotation, Pointf scale) {
this.translation = Objects.requireNonNull(translation, "The translation value must not be null.");
this.scale = Objects.requireNonNull(scale, "The scale value must not be null.");
if (Float.isNaN(rotation)) {
throw new NumberFormatException("The rotation value must not be NaN.");
}
this.rotation = rotation;
return this;
}

public Polygon2D build() {
Polygon2D polygon2D = (Polygon2D) new Polygon2D(points)
.setRenderStyle(renderStyle)
.setShouldRender(shouldRender)
.setTransform(translation, rotation, scale);

switch (renderStyle) {
case Fill: {
return polygon2D.setFill(fillPaint);
}
case Outline: {
return polygon2D.setOutline(outlineStroke, outlineColor);
}
case FillAndOutline: {
return polygon2D.setFill(fillPaint).setOutline(outlineStroke, outlineColor);
}
default: {
throw new IllegalStateException("Invalid render style: " + renderStyle.name());
}
}
}
}

0 comments on commit c535bba

Please sign in to comment.