diff --git a/src/main/java/tech/fastj/graphics/util/DrawUtil.java b/src/main/java/tech/fastj/graphics/util/DrawUtil.java index e64ddfbb..78ed416e 100644 --- a/src/main/java/tech/fastj/graphics/util/DrawUtil.java +++ b/src/main/java/tech/fastj/graphics/util/DrawUtil.java @@ -189,22 +189,6 @@ public static boolean pathEquals(Path2D path1, Path2D path2) { return true; } - /** - * Shorthand for checking equality between two {@link MultipleGradientPaint} objects. - * - * @param mGradientPaint1 The first {@code Paint} specified. - * @param mGradientPaint2 The second {@code Paint} specified. - * @return Whether the two {@code Paint}s are equal. - */ - private static boolean mGradientEquals(MultipleGradientPaint mGradientPaint1, MultipleGradientPaint mGradientPaint2) { - return mGradientPaint1.getTransparency() == mGradientPaint2.getTransparency() - && mGradientPaint1.getTransform().equals(mGradientPaint2.getTransform()) - && mGradientPaint1.getColorSpace().equals(mGradientPaint2.getColorSpace()) - && mGradientPaint1.getCycleMethod().equals(mGradientPaint2.getCycleMethod()) - && Arrays.deepEquals(mGradientPaint1.getColors(), mGradientPaint2.getColors()) - && Arrays.equals(mGradientPaint1.getFractions(), mGradientPaint2.getFractions()); - } - /** * Checks for equality between two {@link Paint} objects as best as possible. * @@ -263,6 +247,22 @@ public static boolean paintEquals(Paint paint1, Paint paint2) { return paint1.equals(paint2); } + /** + * Shorthand for checking equality between two {@link MultipleGradientPaint} objects. + * + * @param mGradientPaint1 The first {@code Paint} specified. + * @param mGradientPaint2 The second {@code Paint} specified. + * @return Whether the two {@code Paint}s are equal. + */ + private static boolean mGradientEquals(MultipleGradientPaint mGradientPaint1, MultipleGradientPaint mGradientPaint2) { + return mGradientPaint1.getTransparency() == mGradientPaint2.getTransparency() + && mGradientPaint1.getTransform().equals(mGradientPaint2.getTransform()) + && mGradientPaint1.getColorSpace().equals(mGradientPaint2.getColorSpace()) + && mGradientPaint1.getCycleMethod().equals(mGradientPaint2.getCycleMethod()) + && Arrays.deepEquals(mGradientPaint1.getColors(), mGradientPaint2.getColors()) + && Arrays.equals(mGradientPaint1.getFractions(), mGradientPaint2.getFractions()); + } + /** * Creates a {@code Pointf} array of 4 points, based on the specified x, y, width, and height floats. *
@@ -455,10 +455,7 @@ public static Pointf[] createBoxFromImage(BufferedImage source) { */ public static Rectangle2D.Float createRect(Pointf[] pts) { if (pts.length != 4) { - FastJEngine.error( - CrashMessages.theGameCrashed("a rectangle creation error."), - new IllegalArgumentException("The length of the parameter point array must be 4.") - ); + throw new IllegalArgumentException("The length of the parameter point array must be 4."); } return new Rectangle2D.Float(pts[0].x, pts[0].y, pts[1].x - pts[0].x, pts[3].y - pts[0].y); @@ -484,7 +481,9 @@ public static Rectangle2D.Float createRectFromImage(BufferedImage source, Pointf */ public static Pointf centerOf(Pointf[] points) { Pointf result = new Pointf(); - for (Pointf p : points) result.add(p); + for (Pointf p : points) { + result.add(p); + } return result.divide(points.length); } diff --git a/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java b/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java index 351f9701..13c4b1d1 100644 --- a/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java +++ b/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java @@ -6,6 +6,7 @@ import java.awt.Color; import java.awt.Font; +import java.awt.Paint; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; @@ -128,6 +129,15 @@ void tryComparePath2Ds_wherePathsAreNotEqual() { ); } + @Test + void comparePaints_withNullPaintParameters() { + Paint paint1 = null; + Color paint2 = Color.red; + + assertFalse(DrawUtil.paintEquals(paint1, paint2), "The two paints should not be equal."); + assertFalse(DrawUtil.paintEquals(paint2, paint1), "The two paints should not be equal."); + } + @Test void checkGenerateBox_withFloatXAndY_andFloatSize() { Pointf[] generatedResult = DrawUtil.createBox(5f, 5f, 35f); @@ -243,6 +253,24 @@ void checkGenerateRectangleFloat_withHandwrittenPointfArray() { assertEquals(expectedRect, actualRect, "The two rectangles should be equal."); } + @Test + void tryGenerateRectangleFloat_withHandwrittenPointfArray_butArraySizeIsNotFour() { + float rectX = 0f; + float rectY = 5f; + float rectWidth = 25f; + float rectHeight = 30f; + + Pointf[] points = { + new Pointf(rectX, rectY), + new Pointf(rectX + rectWidth, rectY), + new Pointf(rectX + rectWidth, rectY + rectHeight) + }; + + Throwable exception = assertThrows(IllegalArgumentException.class, () -> DrawUtil.createRect(points)); + String expectedExceptionMessage = "The length of the parameter point array must be 4."; + assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message."); + } + @Test void checkGenerateRectangleFloat_withPointfArrayFromGeneratedBox() { float rectX = 0f;