Skip to content

Commit 7043c1f

Browse files
committed
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} .
1 parent 136b375 commit 7043c1f

File tree

36 files changed

+735
-736
lines changed

36 files changed

+735
-736
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ void setFont(Font font, int tabs) {
14751475
if (boldItalicFont != null) boldItalicFont.dispose();
14761476
boldFont = italicFont = boldItalicFont = null;
14771477
regularFont = font;
1478-
layout.setText(" ");
1478+
layout.setText(" ");
14791479
layout.setFont(font);
14801480
layout.setStyle(new TextStyle(getFont(SWT.NORMAL), null, null), 0, 0);
14811481
layout.setStyle(new TextStyle(getFont(SWT.BOLD), null, null), 1, 1);

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/Tab.java

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
import org.eclipse.swt.graphics.Color;
4444
import org.eclipse.swt.graphics.Font;
4545
import org.eclipse.swt.graphics.FontData;
46-
import org.eclipse.swt.graphics.GC;
4746
import org.eclipse.swt.graphics.Image;
47+
import org.eclipse.swt.graphics.ImageGcDrawer;
4848
import org.eclipse.swt.graphics.Point;
4949
import org.eclipse.swt.graphics.RGB;
5050
import org.eclipse.swt.graphics.Rectangle;
@@ -1294,60 +1294,58 @@ void disposeExampleWidgets () {
12941294
}
12951295

12961296
Image colorImage (Color color) {
1297-
Image image = new Image (display, IMAGE_SIZE, IMAGE_SIZE);
1298-
GC gc = new GC(image);
1299-
gc.setBackground(color);
1300-
Rectangle bounds = image.getBounds();
1301-
gc.fillRectangle(0, 0, bounds.width, bounds.height);
1302-
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
1303-
gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
1304-
gc.dispose();
1297+
ImageGcDrawer imageGcDrawer = (gc, iwidth, iheight) -> {
1298+
gc.setBackground(color);
1299+
gc.fillRectangle(0, 0, iwidth, iheight);
1300+
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
1301+
gc.drawRectangle(0, 0, iwidth - 1, iheight - 1);
1302+
};
1303+
Image image = new Image (display, imageGcDrawer, IMAGE_SIZE, IMAGE_SIZE);
13051304
return image;
13061305
}
13071306

13081307
Image fontImage (Font font) {
1309-
Image image = new Image (display, IMAGE_SIZE, IMAGE_SIZE);
1310-
GC gc = new GC(image);
1311-
Rectangle bounds = image.getBounds();
1312-
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
1313-
gc.fillRectangle(0, 0, bounds.width, bounds.height);
1314-
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
1315-
gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
1316-
FontData data[] = font.getFontData();
1317-
int style = data[0].getStyle();
1318-
switch (style) {
1319-
case SWT.NORMAL:
1320-
gc.drawLine(3, 3, 3, 8);
1321-
gc.drawLine(4, 3, 7, 8);
1322-
gc.drawLine(8, 3, 8, 8);
1323-
break;
1324-
case SWT.BOLD:
1325-
gc.drawLine(3, 2, 3, 9);
1326-
gc.drawLine(4, 2, 4, 9);
1327-
gc.drawLine(5, 2, 7, 2);
1328-
gc.drawLine(5, 3, 8, 3);
1329-
gc.drawLine(5, 5, 7, 5);
1330-
gc.drawLine(5, 6, 7, 6);
1331-
gc.drawLine(5, 8, 8, 8);
1332-
gc.drawLine(5, 9, 7, 9);
1333-
gc.drawLine(7, 4, 8, 4);
1334-
gc.drawLine(7, 7, 8, 7);
1335-
break;
1336-
case SWT.ITALIC:
1337-
gc.drawLine(6, 2, 8, 2);
1338-
gc.drawLine(7, 3, 4, 8);
1339-
gc.drawLine(3, 9, 5, 9);
1340-
break;
1341-
case SWT.BOLD | SWT.ITALIC:
1342-
gc.drawLine(5, 2, 8, 2);
1343-
gc.drawLine(5, 3, 8, 3);
1344-
gc.drawLine(6, 4, 4, 7);
1345-
gc.drawLine(7, 4, 5, 7);
1346-
gc.drawLine(3, 8, 6, 8);
1347-
gc.drawLine(3, 9, 6, 9);
1348-
break;
1349-
}
1350-
gc.dispose();
1308+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
1309+
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
1310+
gc.fillRectangle(0, 0, iwidth, iheight);
1311+
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
1312+
gc.drawRectangle(0, 0, iwidth - 1, iheight - 1);
1313+
FontData data[] = font.getFontData();
1314+
int style = data[0].getStyle();
1315+
switch (style) {
1316+
case SWT.NORMAL:
1317+
gc.drawLine(3, 3, 3, 8);
1318+
gc.drawLine(4, 3, 7, 8);
1319+
gc.drawLine(8, 3, 8, 8);
1320+
break;
1321+
case SWT.BOLD:
1322+
gc.drawLine(3, 2, 3, 9);
1323+
gc.drawLine(4, 2, 4, 9);
1324+
gc.drawLine(5, 2, 7, 2);
1325+
gc.drawLine(5, 3, 8, 3);
1326+
gc.drawLine(5, 5, 7, 5);
1327+
gc.drawLine(5, 6, 7, 6);
1328+
gc.drawLine(5, 8, 8, 8);
1329+
gc.drawLine(5, 9, 7, 9);
1330+
gc.drawLine(7, 4, 8, 4);
1331+
gc.drawLine(7, 7, 8, 7);
1332+
break;
1333+
case SWT.ITALIC:
1334+
gc.drawLine(6, 2, 8, 2);
1335+
gc.drawLine(7, 3, 4, 8);
1336+
gc.drawLine(3, 9, 5, 9);
1337+
break;
1338+
case SWT.BOLD | SWT.ITALIC:
1339+
gc.drawLine(5, 2, 8, 2);
1340+
gc.drawLine(5, 3, 8, 3);
1341+
gc.drawLine(6, 4, 4, 7);
1342+
gc.drawLine(7, 4, 5, 7);
1343+
gc.drawLine(3, 8, 6, 8);
1344+
gc.drawLine(3, 9, 6, 9);
1345+
break;
1346+
}
1347+
};
1348+
Image image = new Image (display, igc, IMAGE_SIZE, IMAGE_SIZE);
13511349
return image;
13521350
}
13531351

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: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
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;
@@ -325,11 +326,10 @@ static Image createThumbnail(Device device, String name) {
325326
Rectangle src = image.getBounds();
326327
Image result = null;
327328
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, src.x, src.y, src.width, src.height, dest.x, dest.y, dest.width, dest.height);
332-
gc.dispose();
329+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
330+
gc.drawImage(image, src.x, src.y, src.width, src.height, 0, 0, iwidth, iheight);
331+
};
332+
result = new Image(device, igc, 16, 16);
333333
}
334334
if (result != null) {
335335
image.dispose();
@@ -347,16 +347,15 @@ static Image createThumbnail(Device device, String name) {
347347
*
348348
* */
349349
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();
350+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
351+
Pattern pattern = new Pattern(device, 0, 0, iwidth - 1,
352+
iheight - 1, color1, color2);
353+
gc.setBackgroundPattern(pattern);
354+
gc.fillRectangle(0,0,iwidth,iheight);
355+
gc.drawRectangle(0, 0, iwidth - 1, iheight - 1);
356+
pattern.dispose();
357+
};
358+
Image image = new Image(device, igc, width, height);
360359
return image;
361360
}
362361

@@ -368,16 +367,15 @@ static Image createImage(Device device, Color color1, Color color2, int width, i
368367
*
369368
* */
370369
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();
370+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
371+
gc.setBackground(color);
372+
gc.fillRectangle(0,0,iwidth,iheight);
373+
if (color.equals(device.getSystemColor(SWT.COLOR_BLACK))) {
374+
gc.setForeground(device.getSystemColor(SWT.COLOR_WHITE));
375+
}
376+
gc.drawRectangle(0, 0, iwidth - 1, iheight - 1);
377+
};
378+
Image image = new Image(device, igc, 16, 16);
381379
return image;
382380
}
383381

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ 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();
41+
4142
shell.addListener(SWT.Paint, event -> {
4243
GC gc1 = event.gc;
4344
Transform tr = new Transform(display);
@@ -61,6 +62,7 @@ public static void main(String[] args) {
6162
if (!display.readAndDispatch())
6263
display.sleep();
6364
}
65+
6466
image.dispose();
6567
font.dispose();
6668
display.dispose();

0 commit comments

Comments
 (0)