Skip to content

Commit bbdf959

Browse files
committed
Introduce a new Image(Display,Point) constructor
The deprecation of the `Image(Display,Rectangle)` constructor in 1d59663 introduces a lot of verbosity. Where one could previously create an image via: > new Image(display, widget.getBounds()) One now has to store the bounds in a local variable and then call: > new Image(display, r.width, r.height) This approach is also error prone, as one might easily mix up the order of parameters. With this new constructor, this problem can be avoided as one can simply call: > new Image(display, widget.getSize())
1 parent 9412c36 commit bbdf959

File tree

12 files changed

+176
-20
lines changed

12 files changed

+176
-20
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,56 @@ public Image(Device device, int width, int height) {
319319
}
320320
}
321321

322+
/**
323+
* Constructs an empty instance of this class with the width
324+
* (the x coordinate) and height (the y coordinate) of the
325+
* specified point. The result may be drawn upon by creating
326+
* a GC and using any of its drawing operations, as shown in
327+
* the following example:
328+
* <pre>
329+
* Image i = new Image(device, boundsRectangle);
330+
* GC gc = new GC(i);
331+
* gc.drawRectangle(0, 0, 50, 50);
332+
* gc.dispose();
333+
* </pre>
334+
* <p>
335+
* Note: Some platforms may have a limitation on the size
336+
* of image that can be created (size depends on width, height,
337+
* and depth). For example, Windows 95, 98, and ME do not allow
338+
* images larger than 16M.
339+
* </p>
340+
* <p>
341+
* You must dispose the image when it is no longer required.
342+
* </p>
343+
*
344+
* @param device the device on which to create the image
345+
* @param size a point specifying the image's width and height (must not be null)
346+
*
347+
* @exception IllegalArgumentException <ul>
348+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
349+
* <li>ERROR_NULL_ARGUMENT - if the bounds point is null</li>
350+
* <li>ERROR_INVALID_ARGUMENT - if either the points width or height is negative</li>
351+
* </ul>
352+
* @exception SWTError <ul>
353+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
354+
* </ul>
355+
*
356+
* @see #dispose()
357+
* @since 3.133
358+
*/
359+
public Image(Device device, Point size) {
360+
super(device);
361+
if (size == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
362+
NSAutoreleasePool pool = null;
363+
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
364+
try {
365+
init(size.x, size.y);
366+
init();
367+
} finally {
368+
if (pool != null) pool.release();
369+
}
370+
}
371+
322372
/**
323373
* Constructs a new instance of this class based on the
324374
* provided image, with an appearance that varies depending
@@ -529,7 +579,7 @@ private void createRepFromSourceAndApplyFlag(NSBitmapImageRep srcRep, int srcWid
529579
*
530580
* @see #dispose()
531581
*
532-
* @deprecated use {@link Image#Image(Device, int, int)} instead
582+
* @deprecated use {@link Image#Image(Device, int, int)} or {@link Image#Image(Device, Point)} instead
533583
*/
534584
@Deprecated(since = "2025-06", forRemoval = true)
535585
public Image(Device device, Rectangle bounds) {

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,49 @@ public final class Image extends Resource implements Drawable {
214214
* @see #dispose()
215215
*/
216216
public Image(Device device, int width, int height) {
217+
this(device, new Point(width, height));
218+
}
219+
220+
/**
221+
* Constructs an empty instance of this class with the width
222+
* (the x coordinate) and height (the y coordinate) of the
223+
* specified point. The result may be drawn upon by creating
224+
* a GC and using any of its drawing operations, as shown in
225+
* the following example:
226+
* <pre>
227+
* Image i = new Image(device, boundsRectangle);
228+
* GC gc = new GC(i);
229+
* gc.drawRectangle(0, 0, 50, 50);
230+
* gc.dispose();
231+
* </pre>
232+
* <p>
233+
* Note: Some platforms may have a limitation on the size
234+
* of image that can be created (size depends on width, height,
235+
* and depth). For example, Windows 95, 98, and ME do not allow
236+
* images larger than 16M.
237+
* </p>
238+
* <p>
239+
* You must dispose the image when it is no longer required.
240+
* </p>
241+
*
242+
* @param device the device on which to create the image
243+
* @param size a point specifying the image's width and height (must not be null)
244+
*
245+
* @exception IllegalArgumentException <ul>
246+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
247+
* <li>ERROR_NULL_ARGUMENT - if the bounds point is null</li>
248+
* <li>ERROR_INVALID_ARGUMENT - if either the points width or height is negative</li>
249+
* </ul>
250+
* @exception SWTError <ul>
251+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
252+
* </ul>
253+
*
254+
* @see #dispose()
255+
* @since 3.133
256+
*/
257+
public Image(Device device, Point size) {
217258
super(device);
218-
Point size = new Point(width, height);
259+
if (size == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
219260
init(size.x, size.y);
220261
init();
221262
}
@@ -406,7 +447,7 @@ public Image(Device device, Image srcImage, int flag) {
406447
*
407448
* @see #dispose()
408449
*
409-
* @deprecated use {@link Image#Image(Device, int, int)} instead
450+
* @deprecated use {@link Image#Image(Device, int, int)} or {@link Image#Image(Device, Point)} instead
410451
*/
411452
@Deprecated(since = "2025-06", forRemoval = true)
412453
public Image(Device device, Rectangle bounds) {

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,51 @@ public Image(Device device, int width, int height) {
247247
this.device.registerResourceWithZoomSupport(this);
248248
}
249249

250+
/**
251+
* Constructs an empty instance of this class with the width
252+
* (the x coordinate) and height (the y coordinate) of the
253+
* specified point. The result may be drawn upon by creating
254+
* a GC and using any of its drawing operations, as shown in
255+
* the following example:
256+
* <pre>
257+
* Image i = new Image(device, boundsRectangle);
258+
* GC gc = new GC(i);
259+
* gc.drawRectangle(0, 0, 50, 50);
260+
* gc.dispose();
261+
* </pre>
262+
* <p>
263+
* Note: Some platforms may have a limitation on the size
264+
* of image that can be created (size depends on width, height,
265+
* and depth). For example, Windows 95, 98, and ME do not allow
266+
* images larger than 16M.
267+
* </p>
268+
* <p>
269+
* You must dispose the image when it is no longer required.
270+
* </p>
271+
*
272+
* @param device the device on which to create the image
273+
* @param size a point specifying the image's width and height (must not be null)
274+
*
275+
* @exception IllegalArgumentException <ul>
276+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
277+
* <li>ERROR_NULL_ARGUMENT - if the bounds point is null</li>
278+
* <li>ERROR_INVALID_ARGUMENT - if either the points width or height is negative</li>
279+
* </ul>
280+
* @exception SWTError <ul>
281+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
282+
* </ul>
283+
*
284+
* @see #dispose()
285+
* @since 3.133
286+
*/
287+
public Image(Device device, Point size) {
288+
super(device);
289+
if (size == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
290+
this.imageProvider = new PlainImageProviderWrapper(size.x, size.y);
291+
init();
292+
this.device.registerResourceWithZoomSupport(this);
293+
}
294+
250295
/**
251296
* Constructs a new instance of this class based on the
252297
* provided image, with an appearance that varies depending
@@ -393,7 +438,7 @@ public Image(Device device, Image srcImage, int flag) {
393438
*
394439
* @see #dispose()
395440
*
396-
* @deprecated use {@link Image#Image(Device, int, int)} instead
441+
* @deprecated use {@link Image#Image(Device, int, int)} or {@link Image#Image(Device, Point)} instead
397442
*/
398443
@Deprecated(since = "2025-06", forRemoval = true)
399444
public Image(Device device, Rectangle bounds) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public static void main(String[] args) {
5858
label.setImage(null);
5959
image.dispose ();
6060
}
61-
Rectangle rect = group.getBounds();
62-
image = new Image (display, rect.width, rect.height);
61+
image = new Image (display, group.getSize());
6362
GC gc = new GC (image);
6463
boolean success = group.print (gc);
6564
gc.dispose ();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public static void main(String[] args) {
4444
button.addListener(SWT.Selection, event -> {
4545
Point tableSize = table.getSize();
4646
GC gc = new GC(table);
47-
final Image image =
48-
new Image(display, tableSize.x, tableSize.y);
47+
final Image image = new Image(display, tableSize);
4948
gc.copyArea(image, 0, 0);
5049
gc.dispose();
5150

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main (String [] args) {
4040
Image originalImage = toolBar.getBackgroundImage();
4141

4242
Point p = toolBar.getSize();
43-
Image bg = new Image(display, p.x, p.y);
43+
Image bg = new Image(display, p);
4444

4545
GC gc = new GC(bg);
4646
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void main(String[] args) {
7171
shell.setSize(cSize);
7272

7373
shell.open();
74-
Image canvas = new Image(display, cSize.x, cSize.y);
74+
Image canvas = new Image(display, cSize);
7575
GC gc = new GC(canvas);
7676
composite.print(gc);
7777

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ private static Composite canvas(Display display, Shell shell) {
8080
}
8181

8282
private static void snapshot(Display display, Composite composite, String filename) {
83-
Rectangle bounds = composite.getBounds();
84-
Image image = new Image(display, bounds.width, bounds.height);
83+
Image image = new Image(display, composite.getSize());
8584
GC gc = new GC(image);
8685
composite.print(gc);
8786
gc.dispose();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ private static Composite canvas(Display display, Shell shell) {
8383
}
8484

8585
private static void snapshot(Display display, Composite composite, String filename) {
86-
Rectangle bounds = composite.getBounds();
87-
Image image = new Image(display, bounds.width, bounds.height);
86+
Image image = new Image(display, composite.getSize());
8887
GC gc = new GC(image);
8988
composite.print(gc);
9089
gc.dispose();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.eclipse.swt.graphics.Image;
2020
import org.eclipse.swt.graphics.ImageData;
2121
import org.eclipse.swt.graphics.ImageLoader;
22-
import org.eclipse.swt.graphics.Rectangle;
2322
import org.eclipse.swt.layout.FillLayout;
2423
import org.eclipse.swt.layout.RowLayout;
2524
import org.eclipse.swt.widgets.Button;
@@ -69,8 +68,7 @@ public static void main(String[] args) {
6968
}
7069

7170
private static void saveImage(Control control, String filename, int format) {
72-
Rectangle bounds = control.getBounds();
73-
Image image = new Image(control.getDisplay(), bounds.width, bounds.height);
71+
Image image = new Image(control.getDisplay(), control.getSize());
7472
GC gc = new GC(image);
7573
control.print(gc);
7674
gc.dispose();

0 commit comments

Comments
 (0)