Skip to content

Commit

Permalink
(#10) integrate "position" method into linear gradient constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jun 5, 2021
1 parent ff38485 commit 9e40285
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/main/java/tech/fastj/graphics/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public static Font randomFont() {
}

public static LinearGradientPaint randomLinearGradient(Drawable drawable, Boundary start, Boundary end) {
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient().position(drawable, start, end);
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(drawable, start, end);

int randomColorCount = Maths.randomInteger(2, 8);
for (int i = 0; i < randomColorCount; i++) {
Expand All @@ -597,7 +597,7 @@ public static LinearGradientPaint randomLinearGradient(Drawable drawable, Bounda
}

public static LinearGradientPaint randomLinearGradientWithAlpha(Drawable drawable, Boundary start, Boundary end) {
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient().position(drawable, start, end);
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(drawable, start, end);

int randomColorCount = Maths.randomInteger(2, 8);
for (int i = 0; i < randomColorCount; i++) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/tech/fastj/graphics/util/PsdfUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ private static Polygon2D[] parsePsdf(String fileLocation) {
break;
}
case "lg": { // set paint as LinearGradientPaint
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient();
linearGradientBuilder.position(
LinearGradientBuilder linearGradientBuilder = Gradients.linearGradient(
new Pointf(
Float.parseFloat(tokens[1]),
Float.parseFloat(tokens[2])
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/tech/fastj/graphics/util/gradients/Gradients.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package tech.fastj.graphics.util.gradients;

import tech.fastj.math.Pointf;
import tech.fastj.graphics.Boundary;
import tech.fastj.graphics.Drawable;

import java.awt.GradientPaint;
import java.awt.LinearGradientPaint;
import java.awt.RadialGradientPaint;
Expand All @@ -15,8 +19,17 @@ public class Gradients {
*
* @return A {@link LinearGradientPaint} builder.
*/
public static LinearGradientBuilder linearGradient() {
return LinearGradientBuilder.builder();
public static LinearGradientBuilder linearGradient(Drawable drawable, Boundary start, Boundary end) {
return LinearGradientBuilder.builder(drawable, start, end);
}

/**
* Gets a builder instance for creating a {@link LinearGradientPaint}.
*
* @return A {@link LinearGradientPaint} builder.
*/
public static LinearGradientBuilder linearGradient(Pointf start, Pointf end) {
return LinearGradientBuilder.builder(start, end);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.awt.Color;
import java.awt.LinearGradientPaint;
import java.util.Arrays;
import java.util.Objects;

/** A builder class for creating {@link LinearGradientPaint} objects. */
public class LinearGradientBuilder implements GradientBuilder {
Expand All @@ -19,8 +20,15 @@ public class LinearGradientBuilder implements GradientBuilder {
private int count;

/** Initializes a {@code LinearGradientBuilder}'s internals. */
LinearGradientBuilder() {
LinearGradientBuilder(Drawable drawable, Boundary start, Boundary end) {
colors = new Color[Gradients.ColorLimit];
position(drawable, start, end);
}

/** Initializes a {@code LinearGradientBuilder}'s internals. */
LinearGradientBuilder(Pointf start, Pointf end) {
colors = new Color[Gradients.ColorLimit];
position(start, end);
}

/**
Expand All @@ -30,9 +38,8 @@ public class LinearGradientBuilder implements GradientBuilder {
* @param drawable The basis for what the {@code start} and {@code end} end positions evaluate to.
* @param start The starting boundary to create the gradient from.
* @param end The ending boundary to create the gradient from.
* @return The {@code LinearGradientBuilder}, for method chaining.
*/
public LinearGradientBuilder position(Drawable drawable, Boundary start, Boundary end) {
private void position(Drawable drawable, Boundary start, Boundary end) {
if (start.equals(end)) {
FastJEngine.error(
GradientBuilder.GradientCreationError,
Expand All @@ -46,18 +53,15 @@ public LinearGradientBuilder position(Drawable drawable, Boundary start, Boundar

this.from = drawable.getBound(start);
this.to = drawable.getBound(end);

return this;
}

/**
* Sets the starting and ending points for the builder, based on the provided {@link Pointf} values.
*
* @param start The {@code Pointf} defining the starting point of the gradient.
* @param end The {@code Pointf} defining the ending point of the gradient.
* @return The {@code LinearGradientBuilder}, for method chaining.
*/
public LinearGradientBuilder position(Pointf start, Pointf end) {
private void position(Pointf start, Pointf end) {
if (start.equals(end)) {
FastJEngine.error(
GradientBuilder.GradientCreationError,
Expand All @@ -71,14 +75,12 @@ public LinearGradientBuilder position(Pointf start, Pointf end) {

this.from = start;
this.to = end;

return this;
}

/**
* Adds a {@link Color} to the builder to be used in the resulting gradient.
* <p>
* The amount of colors used in a {@link LinearGradientBuilder} not exceed {@link Gradients#ColorLimit}.
* The amount of colors used in a {@link LinearGradientBuilder} cannot exceed {@link Gradients#ColorLimit}.
*
* @param color The {@code Color} being added.
* @return The {@code LinearGradientBuilder}, for method chaining.
Expand All @@ -95,6 +97,37 @@ public LinearGradientBuilder withColor(Color color) {
return this;
}

/**
* Adds several {@link Color}s to the builder to be used in the resulting gradient.
* <p>
* The amount of colors used in a {@link LinearGradientBuilder} cannot exceed {@link Gradients#ColorLimit}.
*
* @param colors The {@code Color}s being added. This parameter must not cause the builder to increase over 8
* colors.
* @return The {@code LinearGradientBuilder}, for method chaining.
*/
public LinearGradientBuilder withColors(Color... colors) {
if (count == Gradients.ColorLimit) {
FastJEngine.error(GradientBuilder.GradientCreationError,
new IllegalStateException("Gradients cannot contain more than " + Gradients.ColorLimit + " colors.")
);
}

if (Arrays.stream(colors).anyMatch(Objects::isNull)) {
FastJEngine.error(GradientBuilder.GradientCreationError,
new IllegalStateException(
"Gradients cannot contain null color values."
+ System.lineSeparator()
+ "Colors: " + Arrays.toString(colors)
)
);
}

System.arraycopy(colors, count, this.colors, 0, count + colors.length);
count += colors.length;
return this;
}

/**
* Creates a new {@link LinearGradientPaint} object, using the data provided by other method calls.
*
Expand Down Expand Up @@ -122,7 +155,16 @@ public LinearGradientPaint build() {
*
* @return The {@code LinearGradientBuilder} instance.
*/
static LinearGradientBuilder builder() {
return new LinearGradientBuilder();
static LinearGradientBuilder builder(Pointf start, Pointf end) {
return new LinearGradientBuilder(start, end);
}

/**
* Gets a new instance of a {@link LinearGradientBuilder}.
*
* @return The {@code LinearGradientBuilder} instance.
*/
static LinearGradientBuilder builder(Drawable drawable, Boundary start, Boundary end) {
return new LinearGradientBuilder(drawable, start, end);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ void checkCreateLinearGradient_withTwoRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.build();
Expand Down Expand Up @@ -91,8 +90,7 @@ void checkCreateLinearGradient_withThreeRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2, randomColor3}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
Expand Down Expand Up @@ -136,8 +134,7 @@ void checkCreateLinearGradient_withFourRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2, randomColor3, randomColor4}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
Expand Down Expand Up @@ -183,8 +180,7 @@ void checkCreateLinearGradient_withFiveRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2, randomColor3, randomColor4, randomColor5}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
Expand Down Expand Up @@ -232,8 +228,7 @@ void checkCreateLinearGradient_withSixRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2, randomColor3, randomColor4, randomColor5, randomColor6}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
Expand Down Expand Up @@ -284,8 +279,7 @@ void checkCreateLinearGradient_withSevenRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2, randomColor3, randomColor4, randomColor5, randomColor6, randomColor7}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
Expand Down Expand Up @@ -338,8 +332,7 @@ void checkCreateLinearGradient_withEightRandomColors_usingRandomLocations() {
new Color[]{randomColor1, randomColor2, randomColor3, randomColor4, randomColor5, randomColor6, randomColor7, randomColor8}
);

LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient()
.position(randomStartingPoint, randomEndingPoint)
LinearGradientPaint actualLinearGradientPaint = Gradients.linearGradient(randomStartingPoint, randomEndingPoint)
.withColor(randomColor1)
.withColor(randomColor2)
.withColor(randomColor3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class PsdfUtilTests {
new Pointf(85f, 25f)
};
private static final Polygon2D expectedHouseRoof = new Polygon2D(expectedHouseRoofMesh);
private static final LinearGradientPaint expectedHouseRoofGradient = Gradients.linearGradient()
.position(expectedHouseRoof, Boundary.TopLeft, Boundary.BottomRight)
private static final LinearGradientPaint expectedHouseRoofGradient = Gradients.linearGradient(expectedHouseRoof, Boundary.TopLeft, Boundary.BottomRight)
.withColor(Color.blue)
.withColor(Color.cyan)
.withColor(Color.yellow)
Expand Down

0 comments on commit 9e40285

Please sign in to comment.