From ee5ef6c0e8d57ead1a09283e41cf24db8a003092 Mon Sep 17 00:00:00 2001 From: al-noori Date: Fri, 28 Nov 2025 14:26:18 +0100 Subject: [PATCH] Replacement of Image(device, int, int) constructor in GraphicsExample All usages of the stated constructor with an additional GC initialization are now replaced by an ImageGcDrawer and the Image(device, gc int, int) constructor afterwards in GraphicsExample. Co-authored-by: Amartya Parijat --- .../swt/examples/graphics/GradientTab.java | 44 +++++++------ .../examples/graphics/GraphicsExample.java | 63 +++++++++++-------- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GradientTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GradientTab.java index 43d27f09bf9..2d1392d7b44 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GradientTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GradientTab.java @@ -19,6 +19,7 @@ import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Path; import org.eclipse.swt.graphics.Pattern; import org.eclipse.swt.graphics.Point; @@ -177,29 +178,26 @@ public void paint(GC gc, int width, int height) { * Height of the drawing surface */ Image createImage(Device device, Color color1, Color color2, int width, int height) { - Image image = new Image(device, width/2, height/2); - GC gc = new GC(image); - Rectangle rect = image.getBounds(); - - Pattern pattern1 = new Pattern(device, rect.x, rect.y, rect.width/2f, rect.height/2f, color1, color2); - gc.setBackgroundPattern(pattern1); - Path path = new Path(device); - path.addRectangle(0, 0, width/4f, height/4f); - path.addRectangle(width/4f, height/4f, width/4f, height/4f); - gc.fillPath(path); - path.dispose(); - - Pattern pattern2 = new Pattern(device, rect.width, 0, rect.width/2f, rect.height/2f, color1, color2); - gc.setBackgroundPattern(pattern2); - path = new Path(device); - path.addRectangle(width/4f, 0, width/4f, height/4f); - path.addRectangle(0, height/4f, width/4f, height/4f); - gc.fillPath(path); - path.dispose(); - - gc.dispose(); - pattern1.dispose(); - pattern2.dispose(); + ImageGcDrawer igc = (gc, iwidth, iheight) -> { + Pattern pattern1 = new Pattern(device, 0, 0, iwidth/2f, iheight/2f, color1, color2); + gc.setBackgroundPattern(pattern1); + Path path = new Path(device); + path.addRectangle(0, 0, width/4f, height/4f); + path.addRectangle(width/4f, height/4f, width/4f, height/4f); + gc.fillPath(path); + path.dispose(); + + Pattern pattern2 = new Pattern(device, iwidth, 0, iwidth/2f, iheight/2f, color1, color2); + gc.setBackgroundPattern(pattern2); + path = new Path(device); + path.addRectangle(width/4f, 0, width/4f, height/4f); + path.addRectangle(0, height/4f, width/4f, height/4f); + gc.fillPath(path); + path.dispose(); + pattern1.dispose(); + pattern2.dispose(); + }; + Image image = new Image(device, igc, width/2, height/2); return image; } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java index 2760b09049f..0e63df94543 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java @@ -28,10 +28,12 @@ import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Path; import org.eclipse.swt.graphics.Pattern; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.internal.TransparencyColorImageGcDrawer; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; @@ -325,11 +327,10 @@ static Image createThumbnail(Device device, String name) { Rectangle src = image.getBounds(); Image result = null; if (src.width != 16 || src.height != 16) { - result = new Image(device, 16, 16); - GC gc = new GC(result); - Rectangle dest = result.getBounds(); - gc.drawImage(image, dest.x, dest.y, dest.width, dest.height); - gc.dispose(); + ImageGcDrawer igc = (gc, iwidth, iheight) -> { + gc.drawImage(image, src.x, src.y, src.width, src.height, 0, 0, iwidth, iheight); + }; + result = new Image(device, igc, 16, 16); } if (result != null) { image.dispose(); @@ -347,19 +348,26 @@ static Image createThumbnail(Device device, String name) { * * */ static Image createImage(Device device, Color color1, Color color2, int width, int height) { - Image image = new Image(device, width, height); - GC gc = new GC(image); - Rectangle rect = image.getBounds(); - Pattern pattern = new Pattern(device, rect.x, rect.y, rect.width - 1, - rect.height - 1, color1, color2); - gc.setBackgroundPattern(pattern); - gc.fillRectangle(rect); - gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); - gc.dispose(); - pattern.dispose(); + ImageGcDrawer igc = (gc, iwidth, iheight) -> { + Pattern pattern = new Pattern(device, 0, 0, iwidth - 1, + iheight - 1, color1, color2); + gc.setBackgroundPattern(pattern); + gc.fillRectangle(0,0,iwidth,iheight); + gc.drawRectangle(0, 0, iwidth - 1, iheight - 1); + pattern.dispose(); + }; + Image image = new Image(device, igc, width, height); return image; } +private static Color getTransparencyColor(Device device, Color color) { + Color transparencyColor = device.getSystemColor(SWT.COLOR_CYAN); + if (transparencyColor.equals(color)) { + transparencyColor = device.getSystemColor(SWT.COLOR_DARK_BLUE); + } + return transparencyColor; +} + /** * Creates an image based on the color provided and returns it. * @@ -368,16 +376,21 @@ static Image createImage(Device device, Color color1, Color color2, int width, i * * */ static Image createImage(Device device, Color color) { - Image image = new Image(device, 16, 16); - GC gc = new GC(image); - gc.setBackground(color); - Rectangle rect = image.getBounds(); - gc.fillRectangle(rect); - if (color.equals(device.getSystemColor(SWT.COLOR_BLACK))) { - gc.setForeground(device.getSystemColor(SWT.COLOR_WHITE)); - } - gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); - gc.dispose(); + @SuppressWarnings("restriction") + ImageGcDrawer iGC = new TransparencyColorImageGcDrawer(getTransparencyColor(device, color)) { + @Override + public void drawOn(GC gc, int imageWidth, int imageHeight) { + gc.setBackground(getTransparencyColor(device, color)); + gc.fillRectangle(0, 0, imageWidth, imageHeight); + gc.setBackground(color); + gc.fillRectangle(0, 0, imageWidth - 1, imageHeight - 1); + if (color.equals(device.getSystemColor(SWT.COLOR_BLACK))) { + gc.setForeground(device.getSystemColor(SWT.COLOR_WHITE)); + } + gc.drawRectangle(0, 0, imageWidth - 1, imageHeight - 1); + } + }; + Image image = new Image (device, iGC, 16, 16); return image; }