/******************************************************************************* * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This Example Content is intended to demonstrate * usage of Eclipse technology. It is provided to you under the terms and * conditions of the Eclipse Distribution License v1.0 which is available * at http://www.eclipse.org/org/documents/edl-v10.php * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.snippets; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; /** * Draw images scaled to 0.5, 1, 1.25, 1.5, and 2 times original size. */ public class SnippetScaling { public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED); shell.setLayout(new RowLayout(SWT.HORIZONTAL)); createImages(createColumn(shell), new Image(display, "bin/org/eclipse/swt/snippets/eclipse16.png")); createImages(createColumn(shell), new Image(display, "bin/org/eclipse/swt/snippets/warning.gif")); createImages(createColumn(shell), display.getSystemImage (SWT.ICON_INFORMATION)); createImages(createColumn(shell), display.getSystemImage (SWT.ICON_WARNING)); createImages(createColumn(shell), display.getSystemImage (SWT.ICON_ERROR)); createImages(createColumn(shell), display.getSystemImage (SWT.ICON_QUESTION)); final Image triangleCircle = new Image(display, 16, 16); GC gc = new GC(triangleCircle); gc.drawPolyline(new int[] { 0,0, 15,0, 0,15, 0,0 }); gc.drawOval(2, 2, 11, 11); gc.dispose(); createImages(createColumn(shell), triangleCircle); shell.pack (); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } private static Composite createColumn(Composite parent) { Composite column = new Composite(parent, SWT.NONE); column.setLayout(new FillLayout(SWT.VERTICAL)); return column; } private static void createImages(Composite parent, final Image image) { Display display = parent.getDisplay(); // ImageData#scaledTo(..) Composite datas = new Composite(parent, SWT.NONE); RowLayout rowLayout = new RowLayout(); rowLayout.marginTop = rowLayout.marginLeft = 10; rowLayout.spacing = 0; datas.setLayout(rowLayout); Rectangle bounds = image.getBounds(); new Label(datas, SWT.NONE).setImage(new Image(display, image.getImageData().scaledTo( (int) Math.round(bounds.width * 0.5) , (int) Math.round(bounds.height * 0.5)))); new Label(datas, SWT.NONE).setImage(image); new Label(datas, SWT.NONE).setImage(new Image(display, image.getImageData().scaledTo( (int) Math.round(bounds.width * 1.25) , (int) Math.round(bounds.height * 1.25)))); new Label(datas, SWT.NONE).setImage(new Image(display, image.getImageData().scaledTo( (int) Math.round(bounds.width * 1.5) , (int) Math.round(bounds.height * 1.5)))); new Label(datas, SWT.NONE).setImage(new Image(display, image.getImageData().scaledTo( bounds.width * 2 , bounds.height * 2))); // Canvas SWT.Paint Canvas paint = new Canvas(parent, SWT.NONE); paint.addListener (SWT.Paint, new Listener () { @Override public void handleEvent (Event e) { Rectangle rect = image.getBounds (); int width = rect.width; int height = rect.height; GC gc = e.gc; int x = 10, y = 10; int w = 0; gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 0.5), (int)Math.round(height * 0.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width, height); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.25), (int)Math.round(height * 1.25)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.5), (int)Math.round(height * 1.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width * 2, height * 2); } }); // Canvas SWT.Paint with Antialias Canvas paintAnti = new Canvas(parent, SWT.NONE); paintAnti.addListener (SWT.Paint, new Listener () { @Override public void handleEvent (Event e) { Rectangle rect = image.getBounds (); int width = rect.width; int height = rect.height; GC gc = e.gc; gc.setAntialias(SWT.ON); int x = 10, y = 10; int w = 0; gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 0.5), (int)Math.round(height * 0.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width, height); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.25), (int)Math.round(height * 1.25)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.5), (int)Math.round(height * 1.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width * 2, height * 2); } }); Canvas paintLow = new Canvas(parent, SWT.NONE); paintLow.addListener (SWT.Paint, new Listener () { @Override public void handleEvent (Event e) { Rectangle rect = image.getBounds (); int width = rect.width; int height = rect.height; GC gc = e.gc; gc.setAntialias(SWT.ON); gc.setInterpolation(SWT.LOW); int x = 10, y = 10; int w = 0; gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 0.5), (int)Math.round(height * 0.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width, height); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.25), (int)Math.round(height * 1.25)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.5), (int)Math.round(height * 1.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width * 2, height * 2); } }); Canvas paintHigh = new Canvas(parent, SWT.NONE); paintHigh.addListener (SWT.Paint, new Listener () { @Override public void handleEvent (Event e) { Rectangle rect = image.getBounds (); int width = rect.width; int height = rect.height; GC gc = e.gc; gc.setAntialias(SWT.ON); gc.setInterpolation(SWT.HIGH); int x = 10, y = 10; int w = 0; gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 0.5), (int)Math.round(height * 0.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width, height); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.25), (int)Math.round(height * 1.25)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = (int)Math.round(width * 1.5), (int)Math.round(height * 1.5)); gc.drawImage (image, 0, 0, width, height, x += w, y, w = width * 2, height * 2); } }); Canvas paintTransform = new Canvas(parent, SWT.NONE); paintTransform.addListener (SWT.Paint, new Listener () { @Override public void handleEvent (Event e) { Rectangle rect = image.getBounds (); int width = rect.width; int height = rect.height; GC gc = e.gc; // gc.setAntialias(SWT.ON); // gc.setInterpolation(SWT.HIGH); int x = 10, y = 10; Transform transform = new Transform(display); transform.scale(0.5f, 0.5f); gc.setTransform(transform); gc.drawImage (image, 0, 0, width, height, Math.round(x / 0.5f), Math.round(y / 0.5f), width, height); x += Math.round(width * 0.5f); transform.dispose(); gc.setTransform(null); gc.drawImage (image, 0, 0, width, height, x, y, width, height); x += width; transform = new Transform(display); transform.scale(1.25f, 1.25f); gc.setTransform(transform); gc.drawImage (image, 0, 0, width, height, Math.round(x / 1.25f), Math.round(y / 1.25f), width, height); x += Math.round(width * 1.25f); transform.dispose(); transform = new Transform(display); transform.scale(1.5f, 1.5f); gc.setTransform(transform); gc.drawImage (image, 0, 0, width, height, Math.round(x / 1.5f), Math.round(y / 1.5f), width, height); x += Math.round(width * 1.5f); transform.dispose(); transform = new Transform(display); transform.scale(2f, 2f); gc.setTransform(transform); gc.drawImage (image, 0, 0, width, height, Math.round(x / 2f), Math.round(y / 2f), width, height); x += Math.round(width * 2f); transform.dispose(); } }); } }