diff --git a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java index 2a15378273d..0aea18091b9 100644 --- a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java +++ b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java @@ -39,7 +39,7 @@ public void setUp() { @Test public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() { - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, width, height) -> {}, 10, 10); int zoom1 = 125; int zoom2 = 150; ImageData imageDataAtZoom1 = image.getImageData(zoom1); @@ -54,7 +54,7 @@ public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() { public void testImageShouldHaveDimesionAsPerZoomLevel() { int zoom = DPIUtil.getDeviceZoom(); int scalingFactor = 2; - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, width, height) -> {}, 10, 10); try { ImageData baseImageData = image.getImageData(zoom); assertEquals("Width should equal the initial width on the same zoom", 10, baseImageData.width); diff --git a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java index 53d23f7e14a..7337fea3f87 100644 --- a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java +++ b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java @@ -36,9 +36,9 @@ import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.dnd.URLTransfer; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.RowLayout; @@ -239,18 +239,17 @@ public void testUrlTransfer() throws InterruptedException { * Creates a DDB test image with a uniform color applied to all pixels. */ private Image createTestImage() { - final Image image = new Image(shell.getDisplay(), 16, 16); try { Color color = shell.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE); - GC gc = new GC(image); - gc.setBackground(color); - gc.fillRectangle(image.getBounds()); - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(color); + gc.fillRectangle(0, 0, width, height); + }; + return new Image(shell.getDisplay(), imageGcDrawer, 16, 16); } catch (Exception e) { - image.dispose(); fail("test image could not be initialized: " + e); } - return image; + return null; } /** diff --git a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug121220_AlphaTransparencyWithPatterns.java b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug121220_AlphaTransparencyWithPatterns.java index 04d60504032..13731af174f 100644 --- a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug121220_AlphaTransparencyWithPatterns.java +++ b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug121220_AlphaTransparencyWithPatterns.java @@ -2,8 +2,8 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Pattern; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; @@ -16,16 +16,15 @@ public static void main(String[] args) { Shell shell = new Shell(disp); shell.setSize(400, 300); shell.setText("Pattern with alpha"); - Image image = new Image(disp, 50, 50); - { - GC gc = new GC(image); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { gc.setForeground(new Color(null, 255, 0, 0)); gc.drawLine(0, 0, 49, 49); gc.drawLine(0, 49, 49, 0); gc.setForeground(new Color(null, 0, 0, 200)); gc.drawString("Pat", 5, 5); - gc.dispose(); - } + }; + Image image = new Image(disp, imageGcDrawer, 50, 50); + final Pattern pat = new Pattern(disp, image); shell.addPaintListener(e -> { diff --git a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug536008_DarkDisabledLabel.java b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug536008_DarkDisabledLabel.java index a3337ec550d..3180e07b690 100644 --- a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug536008_DarkDisabledLabel.java +++ b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug536008_DarkDisabledLabel.java @@ -13,15 +13,24 @@ *******************************************************************************/ package org.eclipse.swt.tests.win32.snippets; +import java.util.function.Consumer; + import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.*; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; +import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.RowLayout; -import org.eclipse.swt.widgets.*; - -import java.util.function.Consumer; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; public class Bug536008_DarkDisabledLabel { static void moveControl(Control control, Point point) { @@ -73,17 +82,13 @@ public static void main (String [] args) { int cx = 60; int cy = 60; int nRow = 0; - - Image image = new Image(display, cx / 2, cy / 2); - { - GC gc = new GC(image); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { gc.setBackground(backColor); gc.setForeground(foreColor); - - Rectangle imageBounds = image.getBounds(); - gc.fillRectangle(imageBounds); - gc.drawOval(imageBounds.x, imageBounds.y, imageBounds.width - 1, imageBounds.height - 1); - } + gc.fillRectangle(0, 0, width, height); + gc.drawOval(0, 0, width - 1, height - 1); + }; + Image image = new Image(display, imageGcDrawer, cx / 2, cy / 2); FillLayout fillLayout = new FillLayout(); fillLayout.marginWidth = 10; diff --git a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560358_DarkMenuBar.java b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560358_DarkMenuBar.java index 22b18eef607..3745aa1c5b3 100644 --- a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560358_DarkMenuBar.java +++ b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560358_DarkMenuBar.java @@ -14,22 +14,28 @@ package org.eclipse.swt.tests.win32.snippets; import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.*; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Device; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.layout.RowLayout; -import org.eclipse.swt.widgets.*; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; public class Bug560358_DarkMenuBar { static Image createMenuImage(Device a_Device) { - Image result = new Image(a_Device, 16, 16); - GC gc = new GC(result); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(a_Device.getSystemColor(SWT.COLOR_BLUE)); + gc.fillRectangle(0, 0, width, height); - gc.setBackground(a_Device.getSystemColor(SWT.COLOR_BLUE)); - gc.fillRectangle(0, 0, 16, 16); + gc.setForeground(a_Device.getSystemColor(SWT.COLOR_RED)); + gc.drawOval(4, 4, 8, 8); + }; + Image result = new Image(a_Device, imageGcDrawer, 16, 16); - gc.setForeground(a_Device.getSystemColor(SWT.COLOR_RED)); - gc.drawOval(4, 4, 8, 8); - - gc.dispose(); return result; } diff --git a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560546_GC_drawString_vs_GC_drawText.java b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560546_GC_drawString_vs_GC_drawText.java index 4929496f617..5fe912fe33c 100644 --- a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560546_GC_drawString_vs_GC_drawText.java +++ b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560546_GC_drawString_vs_GC_drawText.java @@ -13,9 +13,16 @@ *******************************************************************************/ package org.eclipse.swt.tests.win32.snippets; -import org.eclipse.swt.*; -import org.eclipse.swt.graphics.*; -import org.eclipse.swt.widgets.*; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Canvas; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.swt.widgets.Shell; public class Bug560546_GC_drawString_vs_GC_drawText { static Font fontSegoeUI; @@ -73,39 +80,37 @@ void function(GC gc, int x, int y, String string) { static void testSpeeds(Shell shell) { final int finalIterations = 20 * 1000; - String report = ""; - - Image image = new Image(shell.getDisplay(), 2000, 100); - GC gc = new GC(image); - for (int isFinalCalc = 0; isFinalCalc < 2; isFinalCalc++) { - for (int iTestString = 0; iTestString < testStrings.length; iTestString++) { - for (int iTestFunction = 0; iTestFunction < testFunctions.length; iTestFunction++) { - TestString testString = testStrings[iTestString]; - TestFunction testFunction = testFunctions[iTestFunction]; - - // Warm up before measuring - final int iterations = (isFinalCalc != 0) ? finalIterations : 10; - - final long time1 = System.nanoTime(); - for (int iIteration = 0; iIteration < iterations; iIteration++) { - testFunction.function(gc, 0, 0, testString.string); - } - final long time2 = System.nanoTime(); - - if (isFinalCalc != 0) { - final double elapsed = (time2 - time1) / 1000000000.0; - report += String.format("%s, %s - %.3f sec\n", testString.caption, testFunction.caption, elapsed); + ImageGcDrawer gcDrawer = (gc, width, height) -> { + String report = ""; + for (int isFinalCalc = 0; isFinalCalc < 2; isFinalCalc++) { + for (int iTestString = 0; iTestString < testStrings.length; iTestString++) { + for (int iTestFunction = 0; iTestFunction < testFunctions.length; iTestFunction++) { + TestString testString = testStrings[iTestString]; + TestFunction testFunction = testFunctions[iTestFunction]; + + // Warm up before measuring + final int iterations = (isFinalCalc != 0) ? finalIterations : 10; + + final long time1 = System.nanoTime(); + for (int iIteration = 0; iIteration < iterations; iIteration++) { + testFunction.function(gc, 0, 0, testString.string); + } + final long time2 = System.nanoTime(); + + if (isFinalCalc != 0) { + final double elapsed = (time2 - time1) / 1000000000.0; + report += String.format("%s, %s - %.3f sec\n", testString.caption, testFunction.caption, elapsed); + } } } } - } - - gc.dispose(); + MessageBox messageBox = new MessageBox(shell); + messageBox.setMessage(report); + messageBox.open(); + }; + Image image = new Image(shell.getDisplay(), gcDrawer, 2000, 100); + image.getImageData(); image.dispose(); - - MessageBox messageBox = new MessageBox(shell); - messageBox.setMessage(report); - messageBox.open(); } public static void main(String[] args) { diff --git a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug563475_DarkDisabledTable.java b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug563475_DarkDisabledTable.java index 64c5e223e59..5ea0876bce0 100644 --- a/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug563475_DarkDisabledTable.java +++ b/tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug563475_DarkDisabledTable.java @@ -15,9 +15,9 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.internal.win32.OS; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; @@ -52,14 +52,13 @@ static void setColors(Control control, Color backColor, Color foreColor) { static Image createImage(Display display) { Color transparentColor = display.getSystemColor(SWT.COLOR_BLACK); - Image image = new Image(display, 16, 16); - - GC gc = new GC(image); - gc.setBackground(transparentColor); - gc.fillRectangle(image.getBounds()); - gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); - gc.fillRectangle(6, 6, 4, 4); - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(transparentColor); + gc.fillRectangle(0, 0, width, height); + gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); + gc.fillRectangle(6, 6, 4, 4); + }; + Image image = new Image(display, imageGcDrawer, 16, 16); return image; } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java index 122e4a1eade..a2f61c7404b 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java @@ -504,7 +504,7 @@ public static boolean hasPixel(Control control, Color expectedColor) { */ public static boolean hasPixel(Control control, Color expectedColor, Rectangle rect) { GC gc = new GC(control); - final Image image = new Image(control.getDisplay(), control.getSize().x, control.getSize().y); + final Image image = new Image(control.getDisplay(), (iGc, width, height) -> {}, control.getSize().x, control.getSize().y); gc.copyArea(image, 0, 0); gc.dispose(); boolean result = hasPixel(image, expectedColor, rect); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java index f1b9b6aa808..cc561f087c4 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java @@ -36,6 +36,7 @@ import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.LineAttributes; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.Point; @@ -849,20 +850,16 @@ public void test_bug1288_createGCFromImageFromNonDisplayThread() throws Interrup * (16bpp or less). */ RGB getRealRGB(Color color) { - Image colorImage = new Image(display, 10, 10); - GC imageGc = new GC(colorImage); - ImageData imageData; - PaletteData palette; - int pixel; - - imageGc.setBackground(color); - imageGc.setForeground(color); - imageGc.fillRectangle(0, 0, 10, 10); - imageData = colorImage.getImageData(); - palette = imageData.palette; - imageGc.dispose(); + ImageGcDrawer gcDrawer = (imageGc, width, height) -> { + imageGc.setBackground(color); + imageGc.setForeground(color); + imageGc.fillRectangle(0, 0, width, height); + }; + Image colorImage = new Image(display, gcDrawer, 10, 10); + ImageData imageData = colorImage.getImageData(); + PaletteData palette = imageData.palette; colorImage.dispose(); - pixel = imageData.getPixel(0, 0); + int pixel = imageData.getPixel(0, 0); return palette.getRGB(pixel); } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java index c15b45bd454..3f32f4a37b4 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java @@ -208,16 +208,17 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra data = new ImageData(10, 10, 8, new PaletteData(0x30, 0x0C, 0x03)); // set red pixel at x=9, y=9 data.setPixel(9, 9, 0x30); - image = new Image(display, data); - Image gcImage = new Image(display, 10, 10); - GC gc = new GC(gcImage); - gc.drawImage(image, 0, 0); + final Image image2 = new Image(display, data); + ImageGcDrawer gcDrawer = (gc, width, height) -> { + gc.drawImage(image2, 0, 0); + }; + Image gcImage = new Image(display, gcDrawer, 10, 10); ImageData gcImageData = gcImage.getImageData(); int redPixel = gcImageData.getPixel(9, 9); assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel)); - gc.dispose(); gcImage.dispose(); image.dispose(); + image2.dispose(); } @Test @@ -251,21 +252,23 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra data6.setPixel(9, 9, 0x30); data7 = new ImageData(10, 10, 1, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255))); data7.setPixel(9, 9, 1); - image = new Image(display, data6, data7); - Image gcImage = new Image(display, 10, 10); - GC gc = new GC(gcImage); + Image image2 = new Image(display, data6, data7); Color backgroundColor = display.getSystemColor(SWT.COLOR_BLUE); - gc.setBackground(backgroundColor); - gc.fillRectangle(0, 0, 10, 10); - gc.drawImage(image, 0, 0); + final ImageGcDrawer gcDrawer = (gc, width, height) -> { + gc.setBackground(backgroundColor); + gc.fillRectangle(0, 0, 10, 10); + gc.drawImage(image2, 0, 0); + }; + Image gcImage = new Image(display, gcDrawer, 10, 10); + ImageData gcImageData = gcImage.getImageData(); int redPixel = gcImageData.getPixel(9, 9); assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel)); int bluePixel = gcImageData.getPixel(0, 0); assertEquals(getRealRGB(backgroundColor), gcImageData.palette.getRGB(bluePixel)); - gc.dispose(); gcImage.dispose(); image.dispose(); + image2.dispose(); } @Test @@ -543,7 +546,7 @@ public void test_equalsLjava_lang_Object() { @Test public void test_getBackground() { - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, width, height) -> {}, 10, 10); image.dispose(); SWTException e = assertThrows(SWTException.class, () -> image.getBackground()); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); @@ -553,19 +556,19 @@ public void test_getBackground() { @Test public void test_getBounds() { Rectangle bounds = new Rectangle(0, 0, 10, 20); - Image image1 = new Image(display, bounds.width, bounds.height); + Image image1 = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); image1.dispose(); SWTException e = assertThrows(SWTException.class, () -> image1.getBounds()); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); Image image; // creates bitmap image - image = new Image(display, bounds.width, bounds.height); + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); Rectangle bounds1 = image.getBounds(); image.dispose(); assertEquals(bounds, bounds1); - image = new Image(display, bounds.width, bounds.height); + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); bounds1 = image.getBounds(); image.dispose(); assertEquals(bounds, bounds1); @@ -723,21 +726,21 @@ public void test_getImageData_200() { void getImageData_int(int zoom) { Rectangle bounds = new Rectangle(0, 0, 10, 20); - Image image1 = new Image(display, bounds.width, bounds.height); + Image image1 = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); image1.dispose(); SWTException e = assertThrows(SWTException.class, () -> image1.getImageData(zoom)); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); Image image; // creates bitmap image and compare size of imageData - image = new Image(display, bounds.width, bounds.height); + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); ImageData imageDataAtZoom = image.getImageData(zoom); image.dispose(); Rectangle boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height); 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); // creates second bitmap image and compare size of imageData - image = new Image(display, bounds.width, bounds.height); + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); imageDataAtZoom = image.getImageData(zoom); boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height); bounds = image.getBounds(); @@ -793,7 +796,7 @@ public void test_hashCode() { Image image1 = null; try { - image = new Image(display, 10, 10); + image = new Image(display, (gc, width, height) -> {}, 10, 10); image1 = image; assertEquals(image1.hashCode(), image.hashCode()); @@ -854,14 +857,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() { "Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)", SwtTestUtil.isGTK); // TODO Fix GTK failure. - Image image1 = new Image(display, 10, 10); + Image image1 = new Image(display, (gc, width, height) -> {}, 10, 10); try { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> image1.setBackground(null)); assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e); } finally { image1.dispose(); } - Image image2 = new Image(display, 10, 10); + Image image2 = new Image(display, (gc, width, height) -> {}, 10, 10); Color color2 = new Color(255, 255, 255); color2.dispose(); try { @@ -870,14 +873,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() { } finally { image2.dispose(); } - Image image3 = new Image(display, 10, 10); + Image image3 = new Image(display, (gc, width, height) -> {}, 10, 10); image3.dispose(); Color color3 = new Color(255, 255, 255); SWTException e = assertThrows(SWTException.class, () -> image3.setBackground(color3)); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); // this image does not have a transparent pixel by default so setBackground has no effect - Image image4 = new Image(display, 10, 10); + Image image4 = new Image(display, (gc, width, height) -> {}, 10, 10); image4.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); Color color4 = image4.getBackground(); assertNull("background color should be null for non-transparent image", color4); @@ -896,7 +899,7 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() { @Test public void test_toString() { - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, width, height) -> {}, 10, 10); try { assertNotNull(image.toString()); assertTrue(image.toString().length() > 0); @@ -958,23 +961,20 @@ void getImageData2(int depth, PaletteData palette) { } RGB getRealRGB(Color color) { - Image colorImage = new Image(display, 10, 10); - GC imageGc = new GC(colorImage); - ImageData imageData; - PaletteData palette; - int pixel; - - imageGc.setBackground(color); - imageGc.setForeground(color); - imageGc.fillRectangle(0, 0, 10, 10); - imageData = colorImage.getImageData(); - palette = imageData.palette; - imageGc.dispose(); + ImageGcDrawer gcDrawer = (imageGc, width, height) -> { + imageGc.setBackground(color); + imageGc.setForeground(color); + imageGc.fillRectangle(0, 0, width, height); + }; + Image colorImage = new Image(display, gcDrawer, 10, 10); + ImageData imageData = colorImage.getImageData(); + PaletteData palette = imageData.palette; colorImage.dispose(); - pixel = imageData.getPixel(0, 0); + int pixel = imageData.getPixel(0, 0); return palette.getRGB(pixel); } + /** * Create two types of gray-scale image. Same content but one encoded with color * table and one with an efficient use of color masks. @@ -999,15 +999,16 @@ public void test_bug566545_efficientGrayscaleImage() { Image imageIndexed = new Image(display, imageDataIndexed); Image imageDirect = new Image(display, imageDataDirect); - Image outImageIndexed = new Image(display, width, height); - Image outImageDirect = new Image(display, width, height); - GC gc = new GC(outImageIndexed); - gc.drawImage(imageIndexed, 0, 0); - gc.dispose(); - gc = new GC(outImageDirect); - gc.drawImage(imageDirect, 0, 0); - gc.dispose(); + ImageGcDrawer gcDrawer1 = (gc, iWidth, iHeight) -> { + gc.drawImage(imageIndexed, 0, 0); + }; + Image outImageIndexed = new Image(display, gcDrawer1, width, height); + + ImageGcDrawer gcDrawer2 = (gc, iWidth, iHeight) -> { + gc.drawImage(imageDirect, 0, 0); + }; + Image outImageDirect = new Image(display, gcDrawer2, width, height); ImageTestUtil.assertImagesEqual(imageDataIndexed, imageDataDirect); ImageTestUtil.assertImagesEqual(imageIndexed.getImageData(), imageDirect.getImageData()); @@ -1024,7 +1025,7 @@ public void test_updateWidthHeightAfterDPIChange() { int deviceZoom = DPIUtil.getDeviceZoom(); try { Rectangle imageSize = new Rectangle(0, 0, 16, 16); - Image baseImage = new Image(display, imageSize.width, imageSize.height); + Image baseImage = new Image(display, (gc, width, height) -> {}, imageSize.width, imageSize.height); GC gc = new GC(display); gc.drawImage(baseImage, 10, 10); assertEquals("Base image size differs unexpectedly", imageSize, baseImage.getBounds()); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java index 3bf72505b9d..b4a0d8b95a2 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java @@ -26,8 +26,8 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.TextLayout; @@ -938,27 +938,11 @@ public void test_getTextDirection() { public void test_bug568740_multilineTextStyle() { Font font = null; Image image = null; - GC gc = null; - TextLayout layout = null; + final TextLayout layout = new TextLayout(display); + int offset = 10; try { - font = new Font(display, SwtTestUtil.testFontName, 16, SWT.NORMAL); - image = new Image(display, 200, 100); - gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.fillRectangle(image.getBounds()); - gc.setAntialias(SWT.OFF); // aa can change colors and break the test in worst case - - layout = new TextLayout(display); layout.setFont(font); layout.setText("first line\nsecond line"); - - // The test has one multi-line style containing all the problematic properties - // in different colors and a second control style with other colors at the - // end of the second line. Searching for the colors anywhere would even work - // before the bug was fixed. So we search only in the area of the first line and - // if we find any of the control colors we know the search area was calculated - // wrong. - TextStyle style = new TextStyle(); style.borderStyle = SWT.BORDER_DOT; style.borderColor = display.getSystemColor(SWT.COLOR_BLUE); @@ -974,8 +958,23 @@ public void test_bug568740_multilineTextStyle() { controlStyle.strikeoutColor = display.getSystemColor(SWT.COLOR_DARK_RED); layout.setStyle(controlStyle, 15, 23); - int offset = 10; - layout.draw(gc, offset, offset); + font = new Font(display, SwtTestUtil.testFontName, 16, SWT.NORMAL); + final ImageGcDrawer gcDrawer = (gc, width, height) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + gc.fillRectangle(0, 0, width, height); + gc.setAntialias(SWT.OFF); // aa can change colors and break the test in worst case + layout.draw(gc, offset, offset); + + }; + image = new Image(display, gcDrawer, 200, 100); + + + // The test has one multi-line style containing all the problematic properties + // in different colors and a second control style with other colors at the + // end of the second line. Searching for the colors anywhere would even work + // before the bug was fixed. So we search only in the area of the first line and + // if we find any of the control colors we know the search area was calculated + // wrong. Rectangle firstLineBounds = layout.getLineBounds(0); Rectangle searchRangeBorder = new Rectangle(0, 0, image.getBounds().width, offset + (int)(firstLineBounds.height * 0.3)); @@ -993,8 +992,6 @@ public void test_bug568740_multilineTextStyle() { } finally { if (layout != null) layout.dispose(); - if (gc != null) - gc.dispose(); if (image != null) image.dispose(); if (font != null) @@ -1007,21 +1004,17 @@ public void test_bug568740_multilineTextStyle() { * disposed by caller. */ private Image draw(TextLayout layout, int antialias) { - GC gc = null; - try { - Rectangle rect = layout.getBounds(); - Image image = new Image(display, rect.width, rect.height); - gc = new GC(image); + Rectangle rect = layout.getBounds(); + final ImageGcDrawer gcDrawer = (gc, height, width) -> { gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.fillRectangle(image.getBounds()); + gc.fillRectangle(0, 0, width, height); gc.setAntialias(antialias); - layout.draw(gc, 0, 0); - return image; - } finally { - if (gc != null) - gc.dispose(); - } + }; + Image image = new Image(display, gcDrawer, rect.width, rect.height); + + + return image; } /** diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Button.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Button.java index b09fed83200..f3a86da323d 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Button.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Button.java @@ -384,7 +384,7 @@ public void test_setImageLorg_eclipse_swt_graphics_Image() { button.setImage(null); assertNull(button.getImage()); - image = new Image(shell.getDisplay(), 10, 10); + image = new Image(shell.getDisplay(), (gc, width, height) -> {}, 10, 10); button.setImage(image); assertEquals(image, button.getImage()); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Caret.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Caret.java index 7bbc0ee2f1b..84e66c680b0 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Caret.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Caret.java @@ -147,7 +147,7 @@ public void test_setImageLorg_eclipse_swt_graphics_Image() { caret.setImage(null); assertNull(caret.getImage()); - image = new Image(shell.getDisplay(), 10, 10); + image = new Image(shell.getDisplay(), (gc, width, height) -> {}, 10, 10); caret.setImage(image); assertEquals(image, caret.getImage()); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Decorations.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Decorations.java index 556fe2f1873..0793a0a73a0 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Decorations.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Decorations.java @@ -71,7 +71,7 @@ public void test_getDefaultButton() { @Test public void test_getImage() { - Image[] cases = {null, new Image(null, 100, 100)}; + Image[] cases = {null, new Image(null, (gc, width, height) -> {}, 100, 100)}; for (Image image : cases) { decorations.setImage(image); assertEquals(decorations.getImage(), image); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Label.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Label.java index aabe368951c..2356b496b99 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Label.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Label.java @@ -71,7 +71,7 @@ public void test_getAlignment(){ @Test public void test_getImage(){ - Image[] cases = {null, new Image(null, 100, 100)}; + Image[] cases = {null, new Image(null, (gc, width, height) -> {}, 100, 100)}; for (Image image : cases) { label.setImage(image); assertEquals(label.getImage(), image); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug205199_Label_Image_vs_Text.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug205199_Label_Image_vs_Text.java index a8205aeea23..1c5a3da1ddd 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug205199_Label_Image_vs_Text.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug205199_Label_Image_vs_Text.java @@ -15,8 +15,8 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; @@ -55,12 +55,12 @@ public static void main(String[] args) { Composite container = new Composite(shell, SWT.NONE); container.setLayout(new RowLayout(SWT.HORIZONTAL)); - Image image = new Image(display, 32, 32); - Color color = display.getSystemColor(SWT.COLOR_DARK_GREEN); - GC gc = new GC(image); - gc.setBackground(color); - gc.fillRectangle(image.getBounds()); - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + Color color = display.getSystemColor(SWT.COLOR_DARK_GREEN); + gc.setBackground(color); + gc.fillRectangle(0, 0, width, height); + }; + Image image = new Image(display, imageGcDrawer, 32, 32); Label labelText = new Label(container, SWT.NONE); Label labelImage = new Label(container, SWT.NONE); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug529534_CTabFolder_topRight_wrapped.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug529534_CTabFolder_topRight_wrapped.java index dedb750fca2..fd960d91fe5 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug529534_CTabFolder_topRight_wrapped.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug529534_CTabFolder_topRight_wrapped.java @@ -17,8 +17,8 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabFolder; import org.eclipse.swt.custom.CTabItem; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.RowLayout; @@ -96,13 +96,13 @@ public static void main(String[] args) { private static Image image(Display display, int shapeColor) { Rectangle bounds = new Rectangle(0, 0, 16, 16); - Image image = new Image(display, bounds.width, bounds.height); - GC gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); - gc.fillRectangle(bounds); - gc.setBackground(display.getSystemColor(shapeColor)); - gc.fillOval(bounds.x, bounds.y, bounds.width, bounds.height); - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); + gc.fillRectangle(bounds); + gc.setBackground(display.getSystemColor(shapeColor)); + gc.fillOval(bounds.x, bounds.y, bounds.width, bounds.height); + }; + Image image = new Image(display, imageGcDrawer, bounds.width, bounds.height); return image; } } diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug562233_DisposeExceptions.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug562233_DisposeExceptions.java index becc590d9f5..303e2ccd77e 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug562233_DisposeExceptions.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug562233_DisposeExceptions.java @@ -16,8 +16,8 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -249,11 +249,11 @@ public static void main(String[] args) { ); // Make a small image - Image image = new Image (display, 16, 16); - GC gc = new GC(image); - gc.setBackground (display.getSystemColor (SWT.COLOR_BLUE)); - gc.fillRectangle (0, 0, 16, 16); - gc.dispose (); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground (display.getSystemColor (SWT.COLOR_BLUE)); + gc.fillRectangle (0, 0, width, height); + }; + Image image = new Image (display, imageGcDrawer, 16, 16); // And a Table to use it Table table = new Table(shell, 0); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug569752_DetectNonDisposedOsResources.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug569752_DetectNonDisposedOsResources.java index c017927d313..9966fea2e63 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug569752_DetectNonDisposedOsResources.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug569752_DetectNonDisposedOsResources.java @@ -18,7 +18,10 @@ import org.eclipse.swt.graphics.Resource; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.*; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; public class Bug569752_DetectNonDisposedOsResources { public static void main(String[] args) { @@ -41,7 +44,7 @@ public static void main(String[] args) { Button btnTest = new Button(shell, SWT.PUSH); btnTest.setText("Leak Image"); btnTest.addListener(SWT.Selection, event -> { - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, height, width) -> {}, 10, 10); if (chkDispose.getSelection()) image.dispose(); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug577878_GTK_TreeTable_NoDragImageWithPaintItem.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug577878_GTK_TreeTable_NoDragImageWithPaintItem.java index 028e5c5cb6e..b5abbf858e5 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug577878_GTK_TreeTable_NoDragImageWithPaintItem.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug577878_GTK_TreeTable_NoDragImageWithPaintItem.java @@ -14,11 +14,29 @@ package org.eclipse.swt.tests.manual; -import org.eclipse.swt.*; -import org.eclipse.swt.dnd.*; -import org.eclipse.swt.graphics.*; -import org.eclipse.swt.layout.*; -import org.eclipse.swt.widgets.*; +import org.eclipse.swt.SWT; +import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.DragSource; +import org.eclipse.swt.dnd.DragSourceAdapter; +import org.eclipse.swt.dnd.TextTransfer; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Device; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.swt.widgets.TableItem; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeColumn; +import org.eclipse.swt.widgets.TreeItem; public final class Bug577878_GTK_TreeTable_NoDragImageWithPaintItem { static final int NUM_ROWS = 100; @@ -31,17 +49,15 @@ static String makeItemText(int iRow, int iCol) { } static Image makeItemImage(Device device, int iRow) { - Image image = new Image(device, IMG_CX, IMG_CY); - GC gc = new GC(image); - gc.setBackground(new Color(100,255,100)); - gc.fillRectangle(0, 0, IMG_CX-1, IMG_CY-1); - gc.drawRectangle(0, 0, IMG_CX-1, IMG_CY-1); - String text = Integer.toString(iRow); - Point textSize = gc.stringExtent(text); - gc.drawText(text, (IMG_CX - textSize.x) / 2, (IMG_CY - textSize.y) / 2); - - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(new Color(100,255,100)); + gc.fillRectangle(0, 0, width-1, height-1); + gc.drawRectangle(0, 0, width-1, height-1); + Point textSize = gc.stringExtent(text); + gc.drawText(text, (IMG_CX - textSize.x) / 2, (IMG_CY - textSize.y) / 2); + }; + Image image = new Image(device, imageGcDrawer, IMG_CX, IMG_CY); return image; } diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0021_HdcLeak_ImageCreateGdipImage.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0021_HdcLeak_ImageCreateGdipImage.java index a3144523bcf..5cf4591e6d0 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0021_HdcLeak_ImageCreateGdipImage.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0021_HdcLeak_ImageCreateGdipImage.java @@ -15,23 +15,30 @@ package org.eclipse.swt.tests.manual; import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.*; -import org.eclipse.swt.layout.*; -import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageGcDrawer; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Canvas; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; +import org.eclipse.swt.widgets.Shell; public class Issue0021_HdcLeak_ImageCreateGdipImage { static Image makeTrasparentImage(Display display) { - Image image = new Image(display, 40, 40); - GC gc = new GC(image); - Rectangle bounds = image.getBounds(); Color transparent = new Color(display, 0x00, 0x00, 0x00); Color contents = new Color(display, 0x00, 0x80, 0x00); - gc.setBackground(transparent); - gc.fillRectangle(bounds); - gc.setBackground(contents); - gc.fillOval(bounds.x, bounds.y, bounds.width, bounds.height); - gc.dispose(); - + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(transparent); + gc.fillRectangle(0, 0, width, height); + gc.setBackground(contents); + gc.fillOval(0, 0, width, height); + }; + Image image = new Image(display, imageGcDrawer, 40, 40); ImageData imageData = image.getImageData(); imageData.transparentPixel = 0; image.dispose(); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0146_CopyImageToClipBoard.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0146_CopyImageToClipBoard.java index b5466defa7f..0fd0dbc85ce 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0146_CopyImageToClipBoard.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0146_CopyImageToClipBoard.java @@ -19,8 +19,8 @@ import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; @@ -37,14 +37,14 @@ public static void main(String[] args) { int size = 200; // Create a simple image - Image image = new Image(display, size, size); - GC gc = new GC(image); - gc.setBackground(new Color(128, 255, 128)); - gc.setForeground(new Color(0, 0, 0)); - gc.fillRectangle(0, 0, size - 1, size - 1); - gc.drawRectangle(0, 0, size - 1, size - 1); - gc.drawText("Hello World", 10, 10); - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(new Color(128, 255, 128)); + gc.setForeground(new Color(0, 0, 0)); + gc.fillRectangle(0, 0, size - 1, size - 1); + gc.drawRectangle(0, 0, size - 1, size - 1); + gc.drawText("Hello World", 10, 10); + }; + Image image = new Image(display, imageGcDrawer, size, size); // Press the button to copy the image data to the clipboard Button button = new Button(shell, SWT.PUSH); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0603_WrongTaskBarOverlays.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0603_WrongTaskBarOverlays.java index af54efd05ce..98fda0a3f9f 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0603_WrongTaskBarOverlays.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0603_WrongTaskBarOverlays.java @@ -14,11 +14,15 @@ package org.eclipse.swt.tests.manual; -import org.eclipse.swt.*; -import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.layout.*; -import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.ImageGcDrawer; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.TaskItem; public final class Issue0603_WrongTaskBarOverlays { public static void main(String[] args) { @@ -78,14 +82,12 @@ public static void main(String[] args) { } }); - Image image = new Image(display, 16, 16); - { - GC gc = new GC(image); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); - gc.fillRectangle(0, 0, 16, 16); - gc.dispose(); - } + gc.fillRectangle(0, 0, width, height); + }; + Image image = new Image(display, imageGcDrawer, 16, 16); Button chkImage = new Button(shell, SWT.CHECK); chkImage.setText("Set Image overlay for me"); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java index 96424447873..4850b078990 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java @@ -8,8 +8,8 @@ */ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; @@ -20,17 +20,19 @@ public class Snippet36 { public static void main (String [] args) { Display display = new Display(); - Image image1 = new Image (display, 16, 16); Color color = display.getSystemColor (SWT.COLOR_RED); - GC gc = new GC (image1); - gc.setBackground (color); - gc.fillRectangle (image1.getBounds ()); - gc.dispose (); - Image image2 = new Image (display, 200, 200); - gc = new GC (image2); - gc.setBackground (color); - gc.fillRectangle (image2.getBounds ()); - gc.dispose (); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground (color); + gc.fillRectangle (0, 0, width, height); + }; + Image image1 = new Image (display, imageGcDrawer, 16, 16); + + final ImageGcDrawer imageGcDrawer2 = (gc, width, height) -> { + gc.setBackground (color); + gc.fillRectangle (0, 0, width, height); + }; + Image image2 = new Image (display, imageGcDrawer2, 200, 200); + Shell shell = new Shell (display); shell.setText("Snippet 36"); ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER);