Skip to content

Commit 87f04f3

Browse files
al-nooriamartya4256
authored andcommitted
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 <amartya.parijat@yatta.de>
1 parent 9acbc6f commit 87f04f3

2 files changed

Lines changed: 59 additions & 48 deletions

File tree

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GradientTab.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.eclipse.swt.graphics.Device;
2020
import org.eclipse.swt.graphics.GC;
2121
import org.eclipse.swt.graphics.Image;
22+
import org.eclipse.swt.graphics.ImageGcDrawer;
2223
import org.eclipse.swt.graphics.Path;
2324
import org.eclipse.swt.graphics.Pattern;
2425
import org.eclipse.swt.graphics.Point;
@@ -177,29 +178,26 @@ public void paint(GC gc, int width, int height) {
177178
* Height of the drawing surface
178179
*/
179180
Image createImage(Device device, Color color1, Color color2, int width, int height) {
180-
Image image = new Image(device, width/2, height/2);
181-
GC gc = new GC(image);
182-
Rectangle rect = image.getBounds();
183-
184-
Pattern pattern1 = new Pattern(device, rect.x, rect.y, rect.width/2f, rect.height/2f, color1, color2);
185-
gc.setBackgroundPattern(pattern1);
186-
Path path = new Path(device);
187-
path.addRectangle(0, 0, width/4f, height/4f);
188-
path.addRectangle(width/4f, height/4f, width/4f, height/4f);
189-
gc.fillPath(path);
190-
path.dispose();
191-
192-
Pattern pattern2 = new Pattern(device, rect.width, 0, rect.width/2f, rect.height/2f, color1, color2);
193-
gc.setBackgroundPattern(pattern2);
194-
path = new Path(device);
195-
path.addRectangle(width/4f, 0, width/4f, height/4f);
196-
path.addRectangle(0, height/4f, width/4f, height/4f);
197-
gc.fillPath(path);
198-
path.dispose();
199-
200-
gc.dispose();
201-
pattern1.dispose();
202-
pattern2.dispose();
181+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
182+
Pattern pattern1 = new Pattern(device, 0, 0, iwidth/2f, iheight/2f, color1, color2);
183+
gc.setBackgroundPattern(pattern1);
184+
Path path = new Path(device);
185+
path.addRectangle(0, 0, width/4f, height/4f);
186+
path.addRectangle(width/4f, height/4f, width/4f, height/4f);
187+
gc.fillPath(path);
188+
path.dispose();
189+
190+
Pattern pattern2 = new Pattern(device, iwidth, 0, iwidth/2f, iheight/2f, color1, color2);
191+
gc.setBackgroundPattern(pattern2);
192+
path = new Path(device);
193+
path.addRectangle(width/4f, 0, width/4f, height/4f);
194+
path.addRectangle(0, height/4f, width/4f, height/4f);
195+
gc.fillPath(path);
196+
path.dispose();
197+
pattern1.dispose();
198+
pattern2.dispose();
199+
};
200+
Image image = new Image(device, igc, width/2, height/2);
203201
return image;
204202
}
205203

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import org.eclipse.swt.graphics.Device;
2929
import org.eclipse.swt.graphics.GC;
3030
import org.eclipse.swt.graphics.Image;
31+
import org.eclipse.swt.graphics.ImageGcDrawer;
3132
import org.eclipse.swt.graphics.Path;
3233
import org.eclipse.swt.graphics.Pattern;
3334
import org.eclipse.swt.graphics.Point;
3435
import org.eclipse.swt.graphics.Rectangle;
36+
import org.eclipse.swt.internal.TransparencyColorImageGcDrawer;
3537
import org.eclipse.swt.layout.FormAttachment;
3638
import org.eclipse.swt.layout.FormData;
3739
import org.eclipse.swt.layout.FormLayout;
@@ -325,11 +327,10 @@ static Image createThumbnail(Device device, String name) {
325327
Rectangle src = image.getBounds();
326328
Image result = null;
327329
if (src.width != 16 || src.height != 16) {
328-
result = new Image(device, 16, 16);
329-
GC gc = new GC(result);
330-
Rectangle dest = result.getBounds();
331-
gc.drawImage(image, dest.x, dest.y, dest.width, dest.height);
332-
gc.dispose();
330+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
331+
gc.drawImage(image, src.x, src.y, src.width, src.height, 0, 0, iwidth, iheight);
332+
};
333+
result = new Image(device, igc, 16, 16);
333334
}
334335
if (result != null) {
335336
image.dispose();
@@ -347,19 +348,26 @@ static Image createThumbnail(Device device, String name) {
347348
*
348349
* */
349350
static Image createImage(Device device, Color color1, Color color2, int width, int height) {
350-
Image image = new Image(device, width, height);
351-
GC gc = new GC(image);
352-
Rectangle rect = image.getBounds();
353-
Pattern pattern = new Pattern(device, rect.x, rect.y, rect.width - 1,
354-
rect.height - 1, color1, color2);
355-
gc.setBackgroundPattern(pattern);
356-
gc.fillRectangle(rect);
357-
gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
358-
gc.dispose();
359-
pattern.dispose();
351+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
352+
Pattern pattern = new Pattern(device, 0, 0, iwidth - 1,
353+
iheight - 1, color1, color2);
354+
gc.setBackgroundPattern(pattern);
355+
gc.fillRectangle(0,0,iwidth,iheight);
356+
gc.drawRectangle(0, 0, iwidth - 1, iheight - 1);
357+
pattern.dispose();
358+
};
359+
Image image = new Image(device, igc, width, height);
360360
return image;
361361
}
362362

363+
private static Color getTransparencyColor(Device device, Color color) {
364+
Color transparencyColor = device.getSystemColor(SWT.COLOR_CYAN);
365+
if (transparencyColor.equals(color)) {
366+
transparencyColor = device.getSystemColor(SWT.COLOR_DARK_BLUE);
367+
}
368+
return transparencyColor;
369+
}
370+
363371
/**
364372
* Creates an image based on the color provided and returns it.
365373
*
@@ -368,16 +376,21 @@ static Image createImage(Device device, Color color1, Color color2, int width, i
368376
*
369377
* */
370378
static Image createImage(Device device, Color color) {
371-
Image image = new Image(device, 16, 16);
372-
GC gc = new GC(image);
373-
gc.setBackground(color);
374-
Rectangle rect = image.getBounds();
375-
gc.fillRectangle(rect);
376-
if (color.equals(device.getSystemColor(SWT.COLOR_BLACK))) {
377-
gc.setForeground(device.getSystemColor(SWT.COLOR_WHITE));
378-
}
379-
gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
380-
gc.dispose();
379+
@SuppressWarnings("restriction")
380+
ImageGcDrawer iGC = new TransparencyColorImageGcDrawer(getTransparencyColor(device, color)) {
381+
@Override
382+
public void drawOn(GC gc, int imageWidth, int imageHeight) {
383+
gc.setBackground(getTransparencyColor(device, color));
384+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
385+
gc.setBackground(color);
386+
gc.fillRectangle(0, 0, imageWidth - 1, imageHeight - 1);
387+
if (color.equals(device.getSystemColor(SWT.COLOR_BLACK))) {
388+
gc.setForeground(device.getSystemColor(SWT.COLOR_WHITE));
389+
}
390+
gc.drawRectangle(0, 0, imageWidth - 1, imageHeight - 1);
391+
}
392+
};
393+
Image image = new Image (device, iGC, 16, 16);
381394
return image;
382395
}
383396

0 commit comments

Comments
 (0)