Skip to content

Commit b045303

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 6d1e1b3 commit b045303

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,12 @@ private ImageHandle init(ImageData i, int zoom) {
16741674
}
16751675
}
16761676

1677+
private void assertCondition(boolean condition, String message) {
1678+
if (condition) {
1679+
System.err.print(message + "\nImages with custom parts should be created with an ImageGcDrawer. See 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.");
1680+
}
1681+
}
1682+
16771683
/**
16781684
* Invokes platform specific functionality to allocate a new GC handle.
16791685
* <p>
@@ -1704,6 +1710,10 @@ private long configureGC(GCData data, int zoom) {
17041710
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
17051711
}
17061712

1713+
if(Device.strictChecks) {
1714+
assertCondition(imageProvider != null && (imageProvider instanceof ImageDataProviderWrapper || imageProvider instanceof ImageFileNameProviderWrapper), "Image initialized with ImageDataProvider or ImageFileNameProvider is not supposed to be modified.");
1715+
assertCondition(!zoomLevelToImageHandle.isEmpty() && (zoomLevelToImageHandle.size() != 1 || !zoomLevelToImageHandle.containsKey(getZoom())), "Images with handles created for a different zoom level should not be modified.");
1716+
}
17071717
/* Create a compatible HDC for the device */
17081718
long hDC = device.internal_new_GC(null);
17091719
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta Solutions
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Yatta Solutions - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.snippets;
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
22+
* multiple variant for different zoom levels. Mainly motivated by custom
23+
* drawing behavior in the win32 implementation
24+
* <p>
25+
* For a list of all SWT example snippets see
26+
* http://www.eclipse.org/swt/snippets/
27+
* </p>
28+
*/
29+
public class Snippet384 {
30+
31+
public static void main(String[] args) {
32+
System.setProperty("swt.autoScale.updateOnRuntime", "true");
33+
Display display = new Display();
34+
Shell shell = new Shell(display);
35+
shell.setText("Watermark over Image using GC");
36+
shell.setSize(1200, 900);
37+
38+
final ImageFileNameProvider filenameProvider = zoom -> {
39+
String path = null;
40+
switch (zoom) {
41+
case 150:
42+
path = "bin/org/eclipse/swt/snippets/red.jpeg";
43+
break;
44+
case 100:
45+
path = "bin/org/eclipse/swt/snippets/black.jpg";
46+
break;
47+
default:
48+
path = "bin/org/eclipse/swt/snippets/black.jpg";
49+
}
50+
return path;
51+
};
52+
Image loadedImage = new Image(display, filenameProvider);
53+
54+
int width = loadedImage.getBounds().width;
55+
int height = loadedImage.getBounds().height;
56+
57+
Image composedImage = new Image(display, (gc, iX, iY) -> {
58+
gc.drawImage(loadedImage, 0, 0);
59+
60+
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
61+
Font font = new Font(display, "Arial", 24, SWT.BOLD);
62+
gc.setFont(font);
63+
gc.setAlpha(128); // semi-transparent
64+
String watermark = "WATERMARK";
65+
Point extent = gc.textExtent(watermark);
66+
int x = (width - extent.x) / 2;
67+
int y = (height - extent.y) / 2;
68+
gc.drawText(watermark, x, y, SWT.DRAW_TRANSPARENT);
69+
font.dispose();
70+
}, width, height);
71+
72+
shell.addPaintListener(e -> {
73+
e.gc.drawImage(composedImage, 0, 0);
74+
});
75+
76+
shell.open();
77+
while (!shell.isDisposed()) {
78+
if (!display.readAndDispatch())
79+
display.sleep();
80+
}
81+
82+
loadedImage.dispose();
83+
composedImage.dispose();
84+
display.dispose();
85+
}
86+
}
8.59 KB
Loading
1.16 MB
Loading

0 commit comments

Comments
 (0)