Skip to content

Commit 170a372

Browse files
al-nooriamartya4256
authored andcommitted
Replacement of Image(device, int, int) constructor in examples/graphics
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 examples/graphics.
1 parent 7fa0622 commit 170a372

3 files changed

Lines changed: 111 additions & 99 deletions

File tree

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: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
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.ImageData;
32+
import org.eclipse.swt.graphics.ImageDataProvider;
33+
import org.eclipse.swt.graphics.ImageGcDrawer;
34+
import org.eclipse.swt.graphics.PaletteData;
3135
import org.eclipse.swt.graphics.Path;
3236
import org.eclipse.swt.graphics.Pattern;
3337
import org.eclipse.swt.graphics.Point;
38+
import org.eclipse.swt.graphics.RGB;
3439
import org.eclipse.swt.graphics.Rectangle;
3540
import org.eclipse.swt.layout.FormAttachment;
3641
import org.eclipse.swt.layout.FormData;
@@ -325,11 +330,10 @@ static Image createThumbnail(Device device, String name) {
325330
Rectangle src = image.getBounds();
326331
Image result = null;
327332
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();
333+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
334+
gc.drawImage(image, src.x, src.y, src.width, src.height, 0, 0, iwidth, iheight);
335+
};
336+
result = new Image(device, igc, 16, 16);
333337
}
334338
if (result != null) {
335339
image.dispose();
@@ -347,16 +351,15 @@ static Image createThumbnail(Device device, String name) {
347351
*
348352
* */
349353
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();
354+
ImageGcDrawer igc = (gc, iwidth, iheight) -> {
355+
Pattern pattern = new Pattern(device, 0, 0, iwidth - 1,
356+
iheight - 1, color1, color2);
357+
gc.setBackgroundPattern(pattern);
358+
gc.fillRectangle(0,0,iwidth,iheight);
359+
gc.drawRectangle(0, 0, iwidth - 1, iheight - 1);
360+
pattern.dispose();
361+
};
362+
Image image = new Image(device, igc, width, height);
360363
return image;
361364
}
362365

@@ -368,16 +371,29 @@ static Image createImage(Device device, Color color1, Color color2, int width, i
368371
*
369372
* */
370373
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();
374+
ImageDataProvider imageDataProvider = zoom -> {
375+
double scaleFactor = zoom / 100.0;
376+
int w = (int) (16 * scaleFactor);
377+
int h = (int) (16 * scaleFactor);
378+
Color black = device.getSystemColor(SWT.COLOR_BLACK);
379+
Color white = device.getSystemColor(SWT.COLOR_WHITE);
380+
RGB foregroundColor = (color == black ? white : black).getRGB();
381+
PaletteData paletteData = new PaletteData(foregroundColor, color.getRGB());
382+
ImageData imageData = new ImageData(w, h, 1, paletteData);
383+
for (int x = 0; x < w; x++) {
384+
for (int y = 0; y < h; y++) {
385+
if(x == 0 || y == 0 || x == w - 1 || y == h - 1) {
386+
imageData.setPixel(x, y, 0);
387+
}
388+
else {
389+
imageData.setPixel(x, y, 1);
390+
}
391+
}
392+
}
393+
return imageData;
394+
};
395+
396+
Image image = new Image(device, imageDataProvider);
381397
return image;
382398
}
383399

0 commit comments

Comments
 (0)