Skip to content

Commit 9b16a66

Browse files
GC with Image in unsupported use cases
Logging a warning for GC initialized with unsupported use cases. 1. all dynamic images (retrieved via any of the existing providers). 2. all images for which handles in other zoom values have already been created.
1 parent 44f0d56 commit 9b16a66

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
*/
8181
public final class Image extends Resource implements Drawable {
8282

83+
static boolean strictChecks = System.getProperty("org.eclipse.swt.internal.enableStrictChecks") != null;
84+
8385
/**
8486
* specifies whether the receiver is a bitmap or an icon
8587
* (one of <code>SWT.BITMAP</code>, <code>SWT.ICON</code>)
@@ -1690,6 +1692,12 @@ private ImageHandle init(ImageData i, int zoom) {
16901692
}
16911693
}
16921694

1695+
private void assertCondition(boolean condition, String message) {
1696+
if (condition && strictChecks) {
1697+
System.err.print(message + "\nSee https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet384.java for supported variants.");
1698+
}
1699+
}
1700+
16931701
/**
16941702
* Invokes platform specific functionality to allocate a new GC handle.
16951703
* <p>
@@ -1716,6 +1724,9 @@ public long internal_new_GC (GCData data) {
17161724
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
17171725
}
17181726

1727+
assertCondition(imageProvider != null && (imageProvider instanceof ImageDataProviderWrapper || imageProvider instanceof ImageFileNameProviderWrapper), "Image initialized with ImageDataProvider or ImageFileNameProvider is not supposed to be modified.");
1728+
assertCondition(!zoomLevelToImageHandle.isEmpty() && (zoomLevelToImageHandle.size() != 1 || !zoomLevelToImageHandle.containsKey(getZoom())), "Images with handles created for a different zoom level should not be modified.");
1729+
17191730
/* Create a compatible HDC for the device */
17201731
long hDC = device.internal_new_GC(null);
17211732
long imageDC = OS.CreateCompatibleDC(hDC);

examples/org.eclipse.swt.snippets/Snippets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h
214214
- [draw an image scaled to half size and double size](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet355.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet355.png "Preview for Snippet 355")
215215
- [draw an image at various zoom/dpi levels](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet367.png "Preview for Snippet 367")
216216
- [draw a disabled/grayed image at various zoom levels](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet382.png "Preview for Snippet 382")
217+
- [draw an image with ImageGcProvider to draw a watermark over it](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet384.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet384.png "Preview for Snippet 384")
217218

218219
### **ImageData**
219220
- [display an animated GIF](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java)
78.9 KB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.eclipse.swt.snippets;
2+
/*******************************************************************************
3+
* Copyright (c) 2025 Yatta Solutions
4+
*
5+
* This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License 2.0
7+
* which accompanies this distribution, and is available at
8+
* https://www.eclipse.org/legal/epl-2.0/
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
*
12+
* Contributors:
13+
* Yatta Solutions - initial API and implementation
14+
*******************************************************************************/
15+
16+
import org.eclipse.swt.*;
17+
import org.eclipse.swt.graphics.*;
18+
import org.eclipse.swt.widgets.*;
19+
20+
/**
21+
* Snippet to show how to draw on a image loaded from the file system which has multiple variant for different zoom levels.
22+
* Mainly motivated by custom drawing behavior in the win32 implementation
23+
* <p>
24+
* For a list of all SWT example snippets see
25+
* http://www.eclipse.org/swt/snippets/
26+
* </p>
27+
*/
28+
public class Snippet384 {
29+
30+
public static void main(String[] args) {
31+
System.setProperty("swt.autoScale.updateOnRuntime", "true");
32+
Display display = new Display();
33+
Shell shell = new Shell(display);
34+
shell.setText("Watermark over Image using GC");
35+
shell.setSize(1200 ,900);
36+
37+
final ImageFileNameProvider filenameProvider = zoom -> {
38+
String path = null;
39+
switch (zoom) {
40+
case 150:
41+
path = "bin/org/eclipse/swt/snippets/red.jpeg";
42+
break;
43+
case 100:
44+
path = "bin/org/eclipse/swt/snippets/black.jpg";
45+
break;
46+
default:
47+
path = "bin/org/eclipse/swt/snippets/black.jpg";
48+
}
49+
return path;
50+
};
51+
Image loadedImage = new Image(display, filenameProvider);
52+
53+
int width = loadedImage.getBounds().width;
54+
int height = loadedImage.getBounds().height;
55+
56+
Image composedImage = new Image(display, (gc, iX, iY) -> {
57+
gc.drawImage(loadedImage, 0, 0);
58+
59+
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
60+
Font font = new Font(display, "Arial", 24, SWT.BOLD);
61+
gc.setFont(font);
62+
gc.setAlpha(128); // semi-transparent
63+
String watermark = "WATERMARK";
64+
Point extent = gc.textExtent(watermark);
65+
int x = (width - extent.x) / 2;
66+
int y = (height - extent.y) / 2;
67+
gc.drawText(watermark, x, y, SWT.DRAW_TRANSPARENT);
68+
font.dispose();
69+
}, width, height);
70+
71+
shell.addPaintListener(e -> {
72+
e.gc.drawImage(composedImage, 0, 0);
73+
});
74+
75+
shell.open();
76+
while (!shell.isDisposed()) {
77+
if (!display.readAndDispatch()) display.sleep();
78+
}
79+
80+
loadedImage.dispose();
81+
composedImage.dispose();
82+
display.dispose();
83+
}
84+
}
8.59 KB
Loading
1.16 MB
Loading

0 commit comments

Comments
 (0)