|
45 | 45 | * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
46 | 46 | */
|
47 | 47 |
|
48 |
| -public sealed class Rectangle implements Serializable permits MonitorAwareRectangle { |
| 48 | +public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle { |
49 | 49 |
|
50 | 50 | /**
|
51 | 51 | * the x coordinate of the rectangle
|
@@ -356,4 +356,39 @@ public Rectangle union (Rectangle rect) {
|
356 | 356 | return new Rectangle (left, top, right - left, bottom - top);
|
357 | 357 | }
|
358 | 358 |
|
| 359 | +/** |
| 360 | + * Creates a new {@code Rectangle} using the specified top-left point and dimensions. |
| 361 | + * <p> |
| 362 | + * If the provided {@code Point} instance carries additional contextual information, |
| 363 | + * an extended {@code Rectangle} type may be returned to preserve that context. |
| 364 | + * Otherwise, a standard {@code Rectangle} is returned. |
| 365 | + * </p> |
| 366 | + * |
| 367 | + * @param topLeft the top-left corner of the rectangle |
| 368 | + * @param width the width of the rectangle |
| 369 | + * @param height the height of the rectangle |
| 370 | + * @return a new {@code Rectangle} instance appropriate for the given point and dimensions |
| 371 | + * @since 3.131 |
| 372 | + */ |
| 373 | +public static Rectangle of(Point topLeft, int width, int height) { |
| 374 | + if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) { |
| 375 | + return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor()); |
| 376 | + } |
| 377 | + return new Rectangle(topLeft.x, topLeft.y, width, height); |
| 378 | +} |
| 379 | + |
| 380 | +/** |
| 381 | + * Creates and returns a copy of this {@code Rectangle}. |
| 382 | + * <p> |
| 383 | + * This method performs a shallow copy of the rectangle's fields: {@code x}, {@code y}, {@code width}, and {@code height}. |
| 384 | + * It does not copy any subclass-specific fields, so subclasses should override this method if additional fields exist. |
| 385 | + * </p> |
| 386 | + * |
| 387 | + * @return a new {@code Rectangle} instance with the same position and size as this one |
| 388 | + * @since 3.131 |
| 389 | + */ |
| 390 | +@Override |
| 391 | +public Rectangle clone() { |
| 392 | + return new Rectangle(x, y, width, height); |
| 393 | +} |
359 | 394 | }
|
0 commit comments