Skip to content

Commit e125f9e

Browse files
al-nooriakoch-yatta
authored andcommitted
Replacement of Image(device, int, int) constructor for Snippets
Almost 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 for the snippets. This replacement has/could not be made for the snippets {387, 215, 292, 95, 139} and partially not for 141.
1 parent 77f9654 commit e125f9e

29 files changed

+259
-270
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public static void main(String[] args) {
3232
shell.setText("Advanced Graphics");
3333
FontData fd = shell.getFont().getFontData()[0];
3434
final Font font = new Font(display, fd.getName(), 60, SWT.BOLD | SWT.ITALIC);
35-
final Image image = new Image(display, 640, 480);
35+
final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
36+
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
37+
gc.fillOval(0, 0, imageWidth, imageHeight);
38+
};
39+
final Image image = new Image(display,imageGcDrawer, 640, 480);
3640
final Rectangle rect = image.getBounds();
37-
GC gc = new GC(image);
38-
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
39-
gc.fillOval(rect.x, rect.y, rect.width, rect.height);
40-
gc.dispose();
4141
shell.addListener(SWT.Paint, event -> {
4242
GC gc1 = event.gc;
4343
Transform tr = new Transform(display);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public class Snippet104 {
2929
public static void main(String[] args) {
3030
final Display display = new Display();
3131
final int [] count = new int [] {4};
32-
final Image image = new Image(display, 300, 300);
33-
GC gc = new GC(image);
34-
gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
35-
gc.fillRectangle(image.getBounds());
36-
gc.drawText("Splash Screen", 10, 10);
37-
gc.dispose();
32+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
33+
gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
34+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
35+
gc.drawText("Splash Screen", 10, 10);
36+
};
37+
final Image image = new Image(display, imageGcDrawer, 300, 300);
3838
final Shell splash = new Shell(SWT.ON_TOP);
3939
final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
4040
bar.setMaximum(count[0]);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public class Snippet112 {
2828

2929
public static void main (String [] args) {
3030
Display display = new Display ();
31-
final Image image = new Image (display, 20, 20);
32-
Color color = display.getSystemColor (SWT.COLOR_RED);
33-
GC gc = new GC (image);
34-
gc.setBackground (color);
35-
gc.fillRectangle (image.getBounds ());
36-
gc.dispose ();
31+
final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
32+
Color color = display.getSystemColor (SWT.COLOR_RED);
33+
gc.setBackground(color);
34+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
35+
};
36+
final Image image = new Image (display, imageGcDrawer, 20, 20);
3737

3838
Shell shell = new Shell (display);
3939
shell.setText("Snippet 112");

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ public class Snippet138 {
2929
public static void main(String[] args) {
3030
Display display = new Display();
3131

32-
Image small = new Image(display, 16, 16);
33-
GC gc = new GC(small);
34-
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
35-
gc.fillArc(0, 0, 16, 16, 45, 270);
36-
gc.dispose();
32+
final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
33+
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
34+
gc.fillArc(0, 0, imageWidth, imageHeight, 45, 270);
35+
};
36+
Image small = new Image(display, imageGcDrawer, 16, 16);
3737

38-
Image large = new Image(display, 32, 32);
39-
gc = new GC(large);
40-
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
41-
gc.fillArc(0, 0, 32, 32, 45, 270);
42-
gc.dispose();
38+
final ImageGcDrawer imageGcDrawer1 = (gc, imageWidth, imageHeight) -> {
39+
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
40+
gc.fillArc(0, 0, imageWidth, imageHeight, 45, 270);
41+
};
42+
Image large = new Image(display, imageGcDrawer1, 32, 32);
4343

4444
/* Provide different resolutions for icons to get
4545
* high quality rendering wherever the OS needs

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void main(String[] args) {
4646
shellBackground = shell.getBackground();
4747

4848
FileDialog dialog = new FileDialog(shell);
49-
dialog.setFilterExtensions(new String[] {"*.gif"});
49+
dialog.setFilterExtensions("*.gif");
5050
String fileName = dialog.open();
5151
final AtomicBoolean stopAnimation = new AtomicBoolean(false);
5252
if (fileName != null) {
@@ -59,11 +59,14 @@ public static void main(String[] args) {
5959
@SuppressWarnings("unused")
6060
public void run() {
6161
/* Create an off-screen image to draw on, and fill it with the shell background. */
62-
Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight);
62+
int width = loader.logicalScreenWidth;
63+
int height = loader.logicalScreenHeight;
64+
ImageGcDrawer offscreenDrawer = (gc, imageWidth, imageHeight) -> {
65+
gc.setBackground(shellBackground);
66+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
67+
};
68+
Image offScreenImage = new Image(display, offscreenDrawer, width, height);
6369
GC offScreenImageGC = new GC(offScreenImage);
64-
offScreenImageGC.setBackground(shellBackground);
65-
offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);
66-
6770
try {
6871
/* Create the first image and draw it on the off-screen image. */
6972
int imageDataIndex = 0;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public static void main(String[] args) {
3232
Shell shell = new Shell (display);
3333
shell.setText("Snippet 143");
3434
Image image = new Image (display, 16, 16);
35-
Image image2 = new Image (display, 16, 16);
36-
GC gc = new GC(image2);
37-
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
38-
gc.fillRectangle(image2.getBounds());
39-
gc.dispose();
35+
ImageGcDrawer imgc = (gc, iwidth, iheight) -> {
36+
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
37+
gc.fillRectangle(0, 0, iwidth, iheight);
38+
};
39+
Image image2 = new Image(display, imgc, 16, 16);
4040
final Tray tray = display.getSystemTray ();
4141
if (tray == null) {
4242
System.out.println ("The system tray is not available");

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,16 @@ static ImageData convertToSWT(BufferedImage bufferedImage) {
121121
}
122122

123123
static ImageData createSampleImage(Display display) {
124-
Image image = new Image(display, 100, 100);
125-
Rectangle bounds = image.getBounds();
126-
GC gc = new GC(image);
127-
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
128-
gc.fillRectangle(bounds);
129-
gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
130-
gc.fillOval(0, 0, bounds.width, bounds.height);
131-
gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
132-
gc.drawLine(0, 0, bounds.width, bounds.height);
133-
gc.drawLine(bounds.width, 0, 0, bounds.height);
134-
gc.dispose();
124+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
125+
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
126+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
127+
gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
128+
gc.fillOval(0, 0, imageWidth, imageHeight);
129+
gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
130+
gc.drawLine(0, 0, imageWidth, imageHeight);
131+
gc.drawLine(imageWidth, 0, 0, imageHeight);
132+
};
133+
Image image = new Image(display, imageGcDrawer, 100, 100);
135134
ImageData data = image.getImageData();
136135
image.dispose();
137136
return data;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ public void getState (AccessibleControlEvent e) {
8080
}
8181

8282
static Image getStateImage (Display display, boolean checked) {
83-
Image image = new Image (display, 16, 16);
84-
GC gc = new GC (image);
85-
gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW));
86-
gc.fillOval (0, 0, 16, 16);
87-
if (checked) {
88-
gc.setForeground (display.getSystemColor (SWT.COLOR_DARK_GREEN));
89-
gc.drawLine (0, 0, 16, 16);
90-
gc.drawLine (16, 0, 0, 16);
91-
}
92-
gc.dispose ();
83+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
84+
gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
85+
gc.fillOval(0, 0, imageWidth, imageHeight);
86+
if (checked) {
87+
gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
88+
gc.drawLine(0, 0, imageWidth, imageHeight);
89+
gc.drawLine(imageWidth, 0, 0, imageHeight);
90+
}
91+
};
92+
Image image = new Image(display, imageGcDrawer, 16, 16);
9393
return image;
9494
}
9595
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public class Snippet165 {
3232

3333
public static void main (String [] args) {
3434
Display display = new Display ();
35-
Image image = new Image(display, 16, 16);
36-
GC gc = new GC(image);
37-
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
38-
gc.fillRectangle(0, 0, 16, 16);
39-
gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
40-
gc.fillRectangle(3, 3, 10, 10);
41-
gc.dispose();
35+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
36+
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
37+
gc.fillRectangle(0, 0, 16, 16);
38+
gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
39+
gc.fillRectangle(3, 3, 10, 10);
40+
};
41+
Image image = new Image(display, imageGcDrawer, 16, 16);
4242
final Shell shell = new Shell (display);
4343
shell.setText("Snippet 165");
4444
shell.setLayout(new GridLayout());

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ public class Snippet200 {
3030
public static void main(String[] args) {
3131
Display display = new Display();
3232
//define a pattern on an image
33-
final Image image = new Image(display, 1000, 1000);
34-
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
35-
Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
36-
Color white = display.getSystemColor(SWT.COLOR_WHITE);
37-
GC gc = new GC(image);
38-
gc.setBackground(white);
39-
gc.setForeground(yellow);
40-
gc.fillGradientRectangle(0, 0, 1000, 1000, true);
41-
for (int i=-500; i<1000; i+=10) {
42-
gc.setForeground(blue);
43-
gc.drawLine(i, 0, 500 + i, 1000);
44-
gc.drawLine(500 + i, 0, i, 1000);
45-
}
46-
gc.dispose();
33+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
34+
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
35+
Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
36+
Color white = display.getSystemColor(SWT.COLOR_WHITE);
37+
gc.setBackground(white);
38+
gc.setForeground(yellow);
39+
gc.fillGradientRectangle(0, 0, 1000, 1000, true);
40+
for (int i=-500; i<1000; i+=10) {
41+
gc.setForeground(blue);
42+
gc.drawLine(i, 0, 500 + i, 1000);
43+
gc.drawLine(500 + i, 0, i, 1000);
44+
}
45+
};
46+
final Image image = new Image(display, imageGcDrawer, 1000, 1000);
4747
final Pattern pattern;
4848
try {
4949
pattern = new Pattern(display, image);

0 commit comments

Comments
 (0)