Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,33 @@ public static Rectangle of(Point topLeft, int width, int height) {
return new Rectangle(topLeft.x, topLeft.y, width, height);
}

/**
* Creates a new {@code Rectangle} using the specified Point instances for the
* dimensions.
* <p>
* If the provided {@code Point} instance carries additional OfFloat
* information, an extended {@code Rectangle} type may be returned to preserve
* that context. Otherwise, a standard {@code Rectangle} is returned.
* </p>
*
* @param topLeft the top-left corner of the rectangle
* @param dimension the Point(width, height) of the rectangle
* @return a new {@code Rectangle} instance appropriate for the given point and
* dimensions
* @since 3.133
*/
public static Rectangle of(Point topLeft, Point dimension) {
if (topLeft instanceof Point.OfFloat ofFloatTopLeft && dimension instanceof Point.OfFloat ofFloatDimension) {
return new Rectangle.OfFloat(ofFloatTopLeft.getX(), ofFloatTopLeft.getY(), ofFloatDimension.getX(),
ofFloatDimension.getY());
} else if (topLeft instanceof Point.OfFloat ofFloatTopLeft) {
return new Rectangle.OfFloat(ofFloatTopLeft.getX(), ofFloatTopLeft.getY(), dimension.x, dimension.y);
} else if (dimension instanceof Point.OfFloat ofFloatDimension) {
return new Rectangle.OfFloat(topLeft.x, topLeft.y, ofFloatDimension.getX(), ofFloatDimension.getY());
}
return new Rectangle(topLeft.x, topLeft.y, dimension.x, dimension.y);
}

/**
* Creates and returns a copy of this {@code Rectangle}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,66 @@ public void test_unionLorg_eclipse_swt_graphics_Rectangle() {
assertSWTProblem("Incorrect exception thrown for rectangle == null", SWT.ERROR_NULL_ARGUMENT, e);
}

@Test
void test_bothPointsAreOfFloat() {
Point.OfFloat topLeft = new Point.OfFloat(10.5f, 20.5f);
Point.OfFloat dimension = new Point.OfFloat(30.5f, 40.5f);

Rectangle rect = Rectangle.of(topLeft, dimension);

assertTrue(rect instanceof Rectangle.OfFloat);
Rectangle.OfFloat f = (Rectangle.OfFloat) rect;

assertEquals(10.5f, f.getX());
assertEquals(20.5f, f.getY());
assertEquals(30.5f, f.getWidth());
assertEquals(40.5f, f.getHeight());
}

@Test
void testOnlyTopLeftIsOfFloat() {
Point.OfFloat topLeft = new Point.OfFloat(5.5f, 15.5f);
Point dimension = new Point(100, 200);

Rectangle rect = Rectangle.of(topLeft, dimension);

assertTrue(rect instanceof Rectangle.OfFloat);
Rectangle.OfFloat f = (Rectangle.OfFloat) rect;

assertEquals(5.5f, f.getX());
assertEquals(15.5f, f.getY());
assertEquals(100, f.getWidth());
assertEquals(200, f.getHeight());
}

@Test
void testOnlyDimensionIsOfFloat() {
Point topLeft = new Point(7, 9);
Point.OfFloat dimension = new Point.OfFloat(55.5f, 66.5f);

Rectangle rect = Rectangle.of(topLeft, dimension);

assertTrue(rect instanceof Rectangle.OfFloat);
Rectangle.OfFloat f = (Rectangle.OfFloat) rect;

assertEquals(7, f.getX());
assertEquals(9, f.getY());
assertEquals(55.5f, f.getWidth());
assertEquals(66.5f, f.getHeight());
}

@Test
void testNeitherPointIsOfFloat() {
Point topLeft = new Point(1, 2);
Point dimension = new Point(10, 20);

Rectangle rect = Rectangle.of(topLeft, dimension);

assertFalse(rect instanceof Rectangle.OfFloat);
assertEquals(1, rect.x);
assertEquals(2, rect.y);
assertEquals(10, rect.width);
assertEquals(20, rect.height);
}

}
Loading