|
| 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 | +} |
0 commit comments