import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageDataProvider; import org.eclipse.swt.graphics.ImageFileNameProvider; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import java.util.ArrayList; /** * Snippet to test the use of the DPI-aware Image constructors. * Work in progress in https://bugs.eclipse.org/399786 *

* For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */ public class Snippet367 { private static final String IMAGE_100 = "eclipse16.png"; private static final String IMAGE_150 = "eclipse24.png"; private static final String IMAGE_200 = "eclipse32.png"; private static final String IMAGES_ROOT = "bin/org/eclipse/swt/snippets/"; private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100; private static final String IMAGE_PATH_150 = IMAGES_ROOT + IMAGE_150; private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200; public static void main (String [] args) { final ImageFileNameProvider filenameProvider = zoom -> { switch (zoom) { case 100: return IMAGE_PATH_100; case 150: return IMAGE_PATH_150; case 200: return IMAGE_PATH_200; default: return null; } }; final ImageDataProvider imageDataProvider = zoom -> { switch (zoom) { case 100: return new ImageData (IMAGE_PATH_100); case 150: return new ImageData (IMAGE_PATH_150); case 200: return new ImageData (IMAGE_PATH_200); default: return null; } }; final Display display = new Display (); int numWarmupIters = 10; int numIters = numWarmupIters + 200; int totalTests = 0; double totalElapsed = 0; for (int iIter = 0; iIter < numIters; iIter++) { long start = System.nanoTime (); final Shell shell = new Shell (display, SWT.TOOL); shell.setText("Snippet367"); shell.setLayout (new GridLayout (3, false)); Menu menuBar = new Menu(shell, SWT.BAR); shell.setMenuBar(menuBar); MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE); fileItem.setText("&File"); Menu fileMenu = new Menu(menuBar); fileItem.setMenu(fileMenu); MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); exitItem.setText("&Exit"); exitItem.addListener(SWT.Selection, e -> shell.close()); new Label (shell, SWT.NONE).setText (IMAGE_200 + ":"); new Label (shell, SWT.NONE).setImage (trackImage(new Image (display, IMAGE_PATH_200))); new Button(shell, SWT.PUSH).setImage (trackImage(new Image (display, IMAGE_PATH_200))); new Label (shell, SWT.NONE).setText (IMAGE_150 + ":"); new Label (shell, SWT.NONE).setImage (trackImage(new Image (display, IMAGE_PATH_150))); new Button(shell, SWT.NONE).setImage (trackImage(new Image (display, IMAGE_PATH_150))); new Label (shell, SWT.NONE).setText (IMAGE_100 + ":"); new Label (shell, SWT.NONE).setImage (trackImage(new Image (display, IMAGE_PATH_100))); new Button(shell, SWT.NONE).setImage (trackImage(new Image (display, IMAGE_PATH_100))); createSeparator(shell); new Label (shell, SWT.NONE).setText ("ImageFileNameProvider:"); new Label (shell, SWT.NONE).setImage (trackImage(new Image (display, filenameProvider))); new Button(shell, SWT.NONE).setImage (trackImage(new Image (display, filenameProvider))); new Label (shell, SWT.NONE).setText ("ImageDataProvider:"); new Label (shell, SWT.NONE).setImage (trackImage(new Image (display, imageDataProvider))); new Button(shell, SWT.NONE).setImage (trackImage(new Image (display, imageDataProvider))); createSeparator(shell); new Label (shell, SWT.NONE).setText ("1. Canvas\n(PaintListener)"); final Point size = new Point (550, 40); final Canvas canvas = new Canvas (shell, SWT.NONE); canvas.addPaintListener (e -> { Point size1 = canvas.getSize (); paintImage (e.gc, size1); }); GridData gridData = new GridData (size.x, size.y); gridData.horizontalSpan = 2; canvas.setLayoutData (gridData); createSeparator(shell); new Label (shell, SWT.NONE).setText ("2. Painted image\n (default resolution)"); Image image = trackImage(new Image (display, size.x, size.y)); GC gc = new GC (image); try { paintImage (gc, size); } finally { gc.dispose (); } Label imageLabel = new Label (shell, SWT.NONE); imageLabel.setImage (image); imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label (shell, SWT.NONE).setText ("3. Painted image\n(multi-res, unzoomed paint)"); imageLabel = new Label (shell, SWT.NONE); imageLabel.setImage (trackImage(new Image (display, (ImageDataProvider) zoom -> { Image temp = new Image (display, size.x * zoom / 100, size.y * zoom / 100); GC gc1 = new GC (temp); try { paintImage (gc1, size); return temp.getImageData (); } finally { gc1.dispose (); temp.dispose (); } }))); imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label (shell, SWT.NONE).setText ("4. Painted image\n(multi-res, zoomed paint)"); imageLabel = new Label (shell, SWT.NONE); imageLabel.setImage (trackImage(new Image (display, (ImageDataProvider) zoom -> { Image temp = new Image (display, size.x * zoom / 100, size.y * zoom / 100); GC gc1 = new GC (temp); try { paintImage2 (gc1, new Point (size.x * zoom / 100, size.y * zoom / 100), zoom / 100); return temp.getImageData (); } finally { gc1.dispose (); temp.dispose (); } }))); imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")"); Label box= new Label (shell, SWT.NONE); box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); box.setLayoutData (new GridData (50, 50)); shell.pack (); shell.open (); long end = System.nanoTime (); double elapsed = (end - start) / 1000000000.0; if (iIter < numWarmupIters) System.out.format ("#*** Time=%.2fsec Avg=(warming up)%n", elapsed); else { totalElapsed += elapsed; totalTests++; System.out.format ("#%03d Time=%.2fsec Avg=%.2f%n", totalTests, elapsed, totalElapsed / totalTests); } shell.dispose (); disposeAllImages(); System.gc(); } display.dispose(); } private static ArrayList trackedImages = new ArrayList<>(); private static Image trackImage(Image image) { trackedImages.add (image); return image; } private static void disposeAllImages() { for (Image image : trackedImages) { image.dispose (); } trackedImages.clear (); } private static void createSeparator(Composite shell) { new Label (shell, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, false, 3, 1)); } private static void paintImage (GC gc, Point size) { gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND)); gc.fillRectangle (0, 0, size.x, size.y); gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_LIST_SELECTION)); gc.fillRoundRectangle (0, 0, size.x - 1, size.y - 1, 10, 10); gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND)); gc.drawRoundRectangle (0, 0, size.x - 1, size.y - 1, 10, 10); gc.drawText (gc.getFont ().getFontData ()[0].toString (), 10, 10, true); } private static void paintImage2 (GC gc, Point size, int f) { gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND)); gc.fillRectangle (0, 0, size.x, size.y); // Scale line width, corner roundness, and font size. // Caveat: line width expands in all directions, so the origin also has to move. gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_LIST_SELECTION)); gc.fillRoundRectangle (f/2, f/2, size.x - f, size.y - f, 10*f, 10*f); gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND)); gc.setLineWidth (f); gc.drawRoundRectangle (f/2, f/2, size.x - f, size.y - f, 10*f, 10*f); FontData fontData = gc.getFont ().getFontData ()[0]; fontData.setHeight (fontData.getHeight ()*f); Font font = new Font (gc.getDevice (), fontData); try { gc.setFont (font); gc.drawText (fontData.toString (), 10*f, 10*f, true); } finally { font.dispose (); } } }