Skip to content

Commit cf6e76c

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 cf6e76c

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-1
lines changed

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

Lines changed: 50 additions & 0 deletions
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

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

Lines changed: 42 additions & 1 deletion
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
}

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

Lines changed: 45 additions & 0 deletions
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

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,34 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
187187
image.dispose();
188188
}
189189

190+
@Test
191+
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_Point() {
192+
Image image;
193+
IllegalArgumentException e;
194+
195+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, new Point(-1, 10)));
196+
assertSWTProblem("Incorrect exception thrown for width < 0", SWT.ERROR_INVALID_ARGUMENT, e);
197+
198+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, new Point(0, 10)));
199+
assertSWTProblem("Incorrect exception thrown for width == 0", SWT.ERROR_INVALID_ARGUMENT, e);
200+
201+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, new Point(10, -1)));
202+
assertSWTProblem("Incorrect exception thrown for height < 0", SWT.ERROR_INVALID_ARGUMENT, e);
203+
204+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, new Point(10, 0)));
205+
assertSWTProblem("Incorrect exception thrown for height == 0", SWT.ERROR_INVALID_ARGUMENT, e);
206+
207+
e = assertThrows(IllegalArgumentException.class, () -> new Image(display, (Point) null));
208+
assertSWTProblem("Incorrect exception thrown for size == null", SWT.ERROR_NULL_ARGUMENT, e);
209+
210+
// valid images
211+
image = new Image(null, new Point(10, 10));
212+
image.dispose();
213+
214+
image = new Image(display, new Point(10, 10));
215+
image.dispose();
216+
}
217+
190218
@Test
191219
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_ImageData() {
192220
IllegalArgumentException e;

0 commit comments

Comments
 (0)