Skip to content

Commit 587903b

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 062437b commit 587903b

33 files changed

+642
-637
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.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();

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

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,63 @@
2626

2727
public class Snippet104 {
2828

29-
public static void main(String[] args) {
30-
final Display display = new Display();
31-
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();
38-
final Shell splash = new Shell(SWT.ON_TOP);
39-
final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
40-
bar.setMaximum(count[0]);
41-
Label label = new Label(splash, SWT.NONE);
42-
label.setImage(image);
43-
FormLayout layout = new FormLayout();
44-
splash.setLayout(layout);
45-
FormData labelData = new FormData ();
46-
labelData.right = new FormAttachment (100, 0);
47-
labelData.bottom = new FormAttachment (100, 0);
48-
label.setLayoutData(labelData);
49-
FormData progressData = new FormData ();
50-
progressData.left = new FormAttachment (0, 5);
51-
progressData.right = new FormAttachment (100, -5);
52-
progressData.bottom = new FormAttachment (100, -5);
53-
bar.setLayoutData(progressData);
54-
splash.pack();
55-
Rectangle splashRect = splash.getBounds();
56-
Rectangle displayRect = display.getBounds();
57-
int x = (displayRect.width - splashRect.width) / 2;
58-
int y = (displayRect.height - splashRect.height) / 2;
59-
splash.setLocation(x, y);
60-
splash.open();
61-
display.asyncExec(() -> {
62-
Shell [] shells = new Shell[count[0]];
63-
for (int i1=0; i1<count[0]; i1++) {
64-
shells [i1] = new Shell(display);
65-
shells [i1].setSize (300, 300);
66-
shells [i1].addListener(SWT.Close, e -> --count[0]);
67-
bar.setSelection(i1+1);
68-
try {Thread.sleep(1000);} catch (Throwable e) {}
69-
}
70-
splash.close();
71-
image.dispose();
72-
for (int i2=0; i2<count[0]; i2++) {
73-
shells [i2].setText("SWT Snippet 104 - " + (i2+1));
74-
shells [i2].open();
29+
public static void main(String[] args) {
30+
final Display display = new Display();
31+
final int[] count = new int[] { 4 };
32+
33+
ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
34+
gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
35+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
36+
gc.drawText("Splash Screen", 10, 10);
37+
};
38+
final Image image = new Image(display, imageGcDrawer, 300, 300);
39+
final Shell splash = new Shell(SWT.ON_TOP);
40+
final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
41+
bar.setMaximum(count[0]);
42+
Label label = new Label(splash, SWT.NONE);
43+
label.setImage(image);
44+
FormLayout layout = new FormLayout();
45+
splash.setLayout(layout);
46+
FormData labelData = new FormData();
47+
labelData.right = new FormAttachment(100, 0);
48+
labelData.bottom = new FormAttachment(100, 0);
49+
label.setLayoutData(labelData);
50+
FormData progressData = new FormData();
51+
progressData.left = new FormAttachment(0, 5);
52+
progressData.right = new FormAttachment(100, -5);
53+
progressData.bottom = new FormAttachment(100, -5);
54+
bar.setLayoutData(progressData);
55+
splash.pack();
56+
Rectangle splashRect = splash.getBounds();
57+
Rectangle displayRect = display.getBounds();
58+
int x = (displayRect.width - splashRect.width) / 2;
59+
int y = (displayRect.height - splashRect.height) / 2;
60+
splash.setLocation(x, y);
61+
splash.open();
62+
display.asyncExec(() -> {
63+
Shell[] shells = new Shell[count[0]];
64+
for (int i1 = 0; i1 < count[0]; i1++) {
65+
shells[i1] = new Shell(display);
66+
shells[i1].setSize(300, 300);
67+
shells[i1].addListener(SWT.Close, e -> --count[0]);
68+
bar.setSelection(i1 + 1);
69+
try {
70+
Thread.sleep(1000);
71+
} catch (Throwable e) {
72+
}
73+
}
74+
splash.close();
75+
image.dispose();
76+
for (int i2 = 0; i2 < count[0]; i2++) {
77+
shells[i2].setText("SWT Snippet 104 - " + (i2 + 1));
78+
shells[i2].open();
79+
}
80+
});
81+
while (count[0] != 0) {
82+
if (!display.readAndDispatch())
83+
display.sleep();
7584
}
76-
});
77-
while (count [0] != 0) {
78-
if (!display.readAndDispatch ()) display.sleep ();
85+
display.dispose();
7986
}
80-
display.dispose();
81-
}
8287

8388
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ 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);
3231
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 ();
32+
33+
final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
34+
gc.setBackground(color);
35+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
36+
};
37+
38+
final Image image = new Image (display, imageGcDrawer, 20, 20);
3739

3840
Shell shell = new Shell (display);
3941
shell.setText("Snippet 112");

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public class Snippet138 {
2929
public static void main(String[] args) {
3030
Display display = new Display();
31-
31+
/*
3232
Image small = new Image(display, 16, 16);
3333
GC gc = new GC(small);
3434
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
@@ -40,6 +40,21 @@ public static void main(String[] args) {
4040
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
4141
gc.fillArc(0, 0, 32, 32, 45, 270);
4242
gc.dispose();
43+
*/
44+
45+
final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
46+
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
47+
gc.fillArc(0, 0, 16, 16, 45, 270);
48+
};
49+
Image small = new Image(display, imageGcDrawer, 16, 16);
50+
51+
final ImageGcDrawer imageGcDrawer1 = (gc, imageWidth, imageHeight) -> {
52+
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
53+
gc.fillArc(0, 0, 32, 32, 45, 270);
54+
};
55+
Image large = new Image(display, imageGcDrawer1, 32, 32);
56+
57+
4358

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ static ImageData flip(ImageData srcData, boolean vertical) {
9191
public static void main(String[] args) {
9292
Display display = new Display();
9393

94-
// create an image with the word "hello" on it
9594
final Image image0 = new Image(display, 50, 30);
9695
GC gc = new GC(image0);
9796
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
@@ -100,8 +99,9 @@ public static void main(String[] args) {
10099
gc.dispose();
101100

102101
ImageData data = image0.getImageData();
102+
103103
// rotate and flip this image
104-
final Image image1 = new Image(display, rotate(data, SWT.LEFT));
104+
final Image image1 = new Image(display, rotate(data, SWT.LEFT));
105105
final Image image2 = new Image(display, rotate(data, SWT.RIGHT));
106106
final Image image3 = new Image(display, rotate(data, SWT.DOWN));
107107
final Image image4 = new Image(display, flip(data, true));

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

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -49,102 +49,79 @@ public static void main(String[] args) {
4949
dialog.setFilterExtensions(new String[] {"*.gif"});
5050
String fileName = dialog.open();
5151
final AtomicBoolean stopAnimation = new AtomicBoolean(false);
52+
5253
if (fileName != null) {
5354
loader = new ImageLoader();
5455
try {
5556
imageDataArray = loader.load(fileName);
5657
if (imageDataArray.length > 1) {
5758
animateThread = new Thread("Animation") {
5859
@Override
59-
@SuppressWarnings("unused")
6060
public void run() {
61-
/* 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);
63-
GC offScreenImageGC = new GC(offScreenImage);
64-
offScreenImageGC.setBackground(shellBackground);
65-
offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);
66-
61+
Image offScreenImage = null;
6762
try {
68-
/* Create the first image and draw it on the off-screen image. */
63+
int width = loader.logicalScreenWidth;
64+
int height = loader.logicalScreenHeight;
65+
66+
ImageGcDrawer offscreenDrawer = (gc, imageWidth, imageHeight) -> {
67+
gc.setBackground(shellBackground);
68+
gc.fillRectangle(0, 0, imageWidth, imageHeight);
69+
};
70+
offScreenImage = new Image(display, offscreenDrawer, width, height);
71+
GC offScreenImageGC = new GC(offScreenImage);
72+
6973
int imageDataIndex = 0;
7074
ImageData imageData = imageDataArray[imageDataIndex];
75+
7176
if (image != null && !image.isDisposed()) image.dispose();
7277
image = new Image(display, imageData);
73-
offScreenImageGC.drawImage(
74-
image,
75-
0,
76-
0,
77-
imageData.width,
78-
imageData.height,
79-
imageData.x,
80-
imageData.y,
81-
imageData.width,
82-
imageData.height);
83-
84-
/* Now loop through the images, creating and drawing each one
85-
* on the off-screen image before drawing it on the shell. */
78+
offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
79+
imageData.x, imageData.y, imageData.width, imageData.height);
80+
8681
int repeatCount = loader.repeatCount;
8782
while ((loader.repeatCount == 0 || repeatCount > 0) && !stopAnimation.get()) {
8883
switch (imageData.disposalMethod) {
8984
case SWT.DM_FILL_BACKGROUND:
90-
/* Fill with the background color before drawing. */
9185
Color bgColor = null;
9286
if (useGIFBackground && loader.backgroundPixel != -1) {
9387
bgColor = new Color(imageData.palette.getRGB(loader.backgroundPixel));
9488
}
9589
offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
9690
offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
91+
if (bgColor != null) bgColor.dispose();
9792
break;
9893
case SWT.DM_FILL_PREVIOUS:
99-
/* Restore the previous image before drawing. */
100-
offScreenImageGC.drawImage(
101-
image,
102-
0,
103-
0,
104-
imageData.width,
105-
imageData.height,
106-
imageData.x,
107-
imageData.y,
108-
imageData.width,
109-
imageData.height);
94+
offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
95+
imageData.x, imageData.y, imageData.width, imageData.height);
11096
break;
11197
}
11298

11399
imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
114100
imageData = imageDataArray[imageDataIndex];
115101
image.dispose();
116102
image = new Image(display, imageData);
117-
offScreenImageGC.drawImage(
118-
image,
119-
0,
120-
0,
121-
imageData.width,
122-
imageData.height,
123-
imageData.x,
124-
imageData.y,
125-
imageData.width,
126-
imageData.height);
127-
128-
/* Draw the off-screen image to the shell. */
103+
104+
offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
105+
imageData.x, imageData.y, imageData.width, imageData.height);
106+
129107
shellGC.drawImage(offScreenImage, 0, 0);
130108

131-
/* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
132109
try {
133110
int ms = imageData.delayTime * 10;
134111
if (ms < 20) ms += 30;
135112
if (ms < 30) ms += 10;
136113
Thread.sleep(ms);
137114
} catch (InterruptedException e) {
115+
// ignore
138116
}
139117

140-
/* If we have just drawn the last image, decrement the repeat count and start again. */
141118
if (imageDataIndex == imageDataArray.length - 1) repeatCount--;
142119
}
120+
offScreenImageGC.dispose();
143121
} catch (SWTException ex) {
144122
System.out.println("There was an error animating the GIF");
145123
} finally {
146124
if (offScreenImage != null && !offScreenImage.isDisposed()) offScreenImage.dispose();
147-
if (offScreenImageGC != null && !offScreenImageGC.isDisposed()) offScreenImageGC.dispose();
148125
if (image != null && !image.isDisposed()) image.dispose();
149126
}
150127
}
@@ -163,4 +140,4 @@ public void run() {
163140
stopAnimation.set(true);
164141
display.dispose();
165142
}
166-
}
143+
}

0 commit comments

Comments
 (0)