Skip to content

Commit 1d59663

Browse files
ShahzaibIbrahimfedejeanne
authored andcommitted
Deprecate Image(Rectangle) constructor and replace usages
Deprecate Image constructor that accepts Rectangle in favor of Image(display, width, height).
1 parent 5d05f5a commit 1d59663

File tree

12 files changed

+50
-10
lines changed

12 files changed

+50
-10
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java

+3
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,10 @@ private void createRepFromSourceAndApplyFlag(NSBitmapImageRep srcRep, int srcWid
527527
* </ul>
528528
*
529529
* @see #dispose()
530+
*
531+
* @deprecated use {@link Image#Image(Device, int, int)} instead
530532
*/
533+
@Deprecated(since = "2025-06", forRemoval = true)
531534
public Image(Device device, Rectangle bounds) {
532535
super(device);
533536
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java

+3
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,10 @@ public Image(Device device, Image srcImage, int flag) {
403403
* </ul>
404404
*
405405
* @see #dispose()
406+
*
407+
* @deprecated use {@link Image#Image(Device, int, int)} instead
406408
*/
409+
@Deprecated(since = "2025-06", forRemoval = true)
407410
public Image(Device device, Rectangle bounds) {
408411
super(device);
409412
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

+3
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@ public Image(Device device, Image srcImage, int flag) {
335335
* </ul>
336336
*
337337
* @see #dispose()
338+
*
339+
* @deprecated use {@link Image#Image(Device, int, int)} instead
338340
*/
341+
@Deprecated(since = "2025-06", forRemoval = true)
339342
public Image(Device device, Rectangle bounds) {
340343
super(device);
341344
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet215.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static void main(String[] args) {
3838

3939
/* Take the screen shot */
4040
GC gc = new GC(display);
41-
final Image image = new Image(display, display.getBounds());
41+
Rectangle rect = display.getBounds();
42+
final Image image = new Image(display, rect.width, rect.height);
4243
gc.copyArea(image, 0, 0);
4344
gc.dispose();
4445

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet292.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public static void main(String[] args) {
5555
button.addListener (SWT.Selection, e -> {
5656
Image image = label.getImage ();
5757
if (image != null) image.dispose ();
58-
image = new Image (display, group.getBounds ());
58+
Rectangle rect = group.getBounds();
59+
image = new Image (display, rect.width, rect.height);
5960
GC gc = new GC (image);
6061
boolean success = group.print (gc);
6162
gc.dispose ();

tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static Composite canvas(Display display, Shell shell) {
8181

8282
private static void snapshot(Display display, Composite composite, String filename) {
8383
Rectangle bounds = composite.getBounds();
84-
Image image = new Image(display, bounds);
84+
Image image = new Image(display, bounds.width, bounds.height);
8585
GC gc = new GC(image);
8686
composite.print(gc);
8787
gc.dispose();

tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ControlPrintBroken.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static Composite canvas(Display display, Shell shell) {
8484

8585
private static void snapshot(Display display, Composite composite, String filename) {
8686
Rectangle bounds = composite.getBounds();
87-
Image image = new Image(display, bounds);
87+
Image image = new Image(display, bounds.width, bounds.height);
8888
GC gc = new GC(image);
8989
composite.print(gc);
9090
gc.dispose();

tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.eclipse.swt.graphics.Image;
2121
import org.eclipse.swt.graphics.ImageData;
2222
import org.eclipse.swt.graphics.ImageLoader;
23+
import org.eclipse.swt.graphics.Rectangle;
2324
import org.eclipse.swt.layout.FillLayout;
2425
import org.eclipse.swt.layout.RowLayout;
2526
import org.eclipse.swt.widgets.Button;
@@ -72,7 +73,8 @@ public void widgetSelected(SelectionEvent e) {
7273
}
7374

7475
private static void saveImage(Control control, String filename, int format) {
75-
Image image = new Image(control.getDisplay(), control.getBounds());
76+
Rectangle bounds = control.getBounds();
77+
Image image = new Image(control.getDisplay(), bounds.width, bounds.height);
7678
GC gc = new GC(image);
7779
control.print(gc);
7880
gc.dispose();

tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547557_ShellPrintGC.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void widgetSelected(SelectionEvent e) {
6666

6767
private static void saveImage(Shell shell, String filename, int format) {
6868
Rectangle bounds = shell.getBounds();
69-
Image image = new Image(shell.getDisplay(), bounds);
69+
Image image = new Image(shell.getDisplay(), bounds.width, bounds.height);
7070
// Printing the client area will result in a warning and only the client area being printed
7171
// Image image = new Image(shell.getDisplay(), shell.getClientArea());
7272
GC gc = new GC(image);

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceII() {
119119
image.dispose();
120120
}
121121

122+
@SuppressWarnings("removal")
122123
@Test
123124
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_Rectangle() {
124125
Image image;
@@ -156,6 +157,31 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
156157
image.dispose();
157158
}
158159

160+
@Test
161+
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_int_int() {
162+
Image image;
163+
IllegalArgumentException e;
164+
165+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, -1, 10));
166+
assertSWTProblem("Incorrect exception thrown for width < 0", SWT.ERROR_INVALID_ARGUMENT, e);
167+
168+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, 0, 10));
169+
assertSWTProblem("Incorrect exception thrown for width == 0", SWT.ERROR_INVALID_ARGUMENT, e);
170+
171+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, 10, -1));
172+
assertSWTProblem("Incorrect exception thrown for height < 0", SWT.ERROR_INVALID_ARGUMENT, e);
173+
174+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, 10, 0));
175+
assertSWTProblem("Incorrect exception thrown for height == 0", SWT.ERROR_INVALID_ARGUMENT, e);
176+
177+
// valid images
178+
image = new Image(null, 10, 10);
179+
image.dispose();
180+
181+
image = new Image(display, 10, 10);
182+
image.dispose();
183+
}
184+
159185
@Test
160186
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_ImageData() {
161187
IllegalArgumentException e;
@@ -521,7 +547,7 @@ public void test_getBounds() {
521547
image.dispose();
522548
assertEquals(bounds, bounds1);
523549

524-
image = new Image(display, bounds);
550+
image = new Image(display, bounds.width, bounds.height);
525551
bounds1 = image.getBounds();
526552
image.dispose();
527553
assertEquals(bounds, bounds1);
@@ -693,7 +719,7 @@ void getImageData_int(int zoom) {
693719
assertEquals(":a: Size of ImageData returned from Image.getImageData(int) method doesn't return matches with bounds in Pixel values.", scaleBounds(bounds, zoom, 100), boundsAtZoom);
694720

695721
// creates second bitmap image and compare size of imageData
696-
image = new Image(display, bounds);
722+
image = new Image(display, bounds.width, bounds.height);
697723
imageDataAtZoom = image.getImageData(zoom);
698724
boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height);
699725
bounds = image.getBounds();

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,8 @@ public void test_bug568740_multilineTextStyle() {
10091009
private Image draw(TextLayout layout, int antialias) {
10101010
GC gc = null;
10111011
try {
1012-
Image image = new Image(display, layout.getBounds());
1012+
Rectangle rect = layout.getBounds();
1013+
Image image = new Image(display, rect.width, rect.height);
10131014
gc = new GC(image);
10141015
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
10151016
gc.fillRectangle(image.getBounds());

tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug529534_CTabFolder_topRight_wrapped.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static void main(String[] args) {
9696

9797
private static Image image(Display display, int shapeColor) {
9898
Rectangle bounds = new Rectangle(0, 0, 16, 16);
99-
Image image = new Image(display, bounds);
99+
Image image = new Image(display, bounds.width, bounds.height);
100100
GC gc = new GC(image);
101101
gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
102102
gc.fillRectangle(bounds);

0 commit comments

Comments
 (0)