Skip to content

Commit 9602bf0

Browse files
PhillipusHeikoKlare
authored andcommitted
Add snippet to compare SVG image loading using three Image constructors
1 parent bbb21e5 commit 9602bf0

File tree

4 files changed

+314
-0
lines changed

4 files changed

+314
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ To contribute a new snippet, [create a snippet contribution as a pull request](h
218218
- [compare algorithms for rendering disabled images](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")
219219
- [draw an image with watermark using ImageGcProvider](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet385.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet385.png "Preview for Snippet 385")
220220
- [options for programmatically creating an image](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet387.java)[(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet387.png "Preview for Snippet 387")
221+
- [draw an image from an SVG file](https://github.com/eclipse-platform/eclipse.platform.swt/tree/master/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet390.java) - [(preview)](https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/examples/org.eclipse.swt.snippets/previews/Snippet390.png "Preview for Snippet 390")
221222

222223
### **ImageData**
223224
- [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)
23.6 KB
Loading
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Eclipse Platform Contributors and others.
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+
* Eclipse Platform Contributors - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.snippets;
15+
16+
import java.nio.file.Path;
17+
18+
import org.eclipse.swt.graphics.*;
19+
import org.eclipse.swt.layout.*;
20+
import org.eclipse.swt.widgets.*;
21+
22+
/**
23+
* Snippet to draw an SVG image using three Image constructors
24+
* <p>
25+
* Useful for comparing rendering on different operating systems.
26+
* <p>
27+
* For a list of all SWT example snippets see
28+
* http://www.eclipse.org/swt/snippets/
29+
*
30+
* @see org.eclipse.swt.graphics.Image#Image(org.eclipse.swt.graphics.Device,
31+
* java.io.InputStream)
32+
* @see org.eclipse.swt.graphics.Image#Image(org.eclipse.swt.graphics.Device,
33+
* String)
34+
* @see org.eclipse.swt.graphics.Image#Image(org.eclipse.swt.graphics.Device,
35+
* org.eclipse.swt.graphics.ImageFileNameProvider)
36+
*/
37+
public class Snippet390 {
38+
39+
public static void main(String[] args) throws Exception {
40+
final Display display = new Display();
41+
42+
final Shell shell = new Shell(display);
43+
shell.setText("Snippet 390");
44+
shell.setLayout(new FillLayout());
45+
shell.setBounds(250, 50, 1000, 400);
46+
shell.setBackground(new Color(255, 255, 255));
47+
48+
String imgPath = "eclipse.svg";
49+
Path fullPath = Path.of(Snippet390.class.getResource(imgPath).toURI());
50+
51+
// Image loaded in stream
52+
Image image1 = new Image(display, Snippet390.class.getResourceAsStream(imgPath));
53+
54+
// Image loaded in file path
55+
Image image2 = new Image(display, fullPath.toString());
56+
57+
// Image loaded in ImageFileNameProvider
58+
Image image3 = new Image(display, (ImageFileNameProvider) zoom -> {
59+
return zoom == 100 ? fullPath.toString() : null;
60+
});
61+
62+
shell.addPaintListener(e -> {
63+
e.gc.drawImage(image1, 20, 10, 300, 300);
64+
e.gc.drawText("Image(Display, InputStream)", 80, 300);
65+
66+
e.gc.drawImage(image2, 340, 10, 300, 300);
67+
e.gc.drawText("Image(Display, String)", 420, 300);
68+
69+
e.gc.drawImage(image3, 660, 10, 300, 300);
70+
e.gc.drawText("Image(Display, ImageFileNameProvider)", 700, 300);
71+
});
72+
73+
shell.open();
74+
75+
while (!shell.isDisposed()) {
76+
if (!display.readAndDispatch()) {
77+
display.sleep();
78+
}
79+
}
80+
81+
display.dispose();
82+
}
83+
}
Lines changed: 230 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)