1 /******************************************************************************* 2 * Copyright (c) 2000, 2016 IBM Corporation 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 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.swt.snippets; 15 16 /* 17 * Use transformation matrices to reflect, rotate and shear images 18 * 19 * For a list of all SWT example snippets see 20 * http://www.eclipse.org/swt/snippets/ 21 */ 22 import org.eclipse.swt.*; 23 import org.eclipse.swt.graphics.*; 24 import org.eclipse.swt.layout.*; 25 import org.eclipse.swt.widgets.*; 26 27 public class Snippet207 { main(String[] args)28 public static void main(String[] args) { 29 final Display display = new Display(); 30 31 final Image image = new Image(display, 110, 60); 32 GC gc = new GC(image); 33 Font font = new Font(display, "Times", 30, SWT.BOLD); 34 gc.setFont(font); 35 gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); 36 gc.fillRectangle(0, 0, 110, 60); 37 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); 38 gc.drawText("SWT", 10, 10, true); 39 font.dispose(); 40 gc.dispose(); 41 42 final Rectangle rect = image.getBounds(); 43 Shell shell = new Shell(display); 44 shell.setText("Matrix Tranformations"); 45 shell.setLayout(new FillLayout()); 46 final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED); 47 canvas.addPaintListener(e -> { 48 GC gc1 = e.gc; 49 gc1.setAdvanced(true); 50 if (!gc1.getAdvanced()){ 51 gc1.drawText("Advanced graphics not supported", 30, 30, true); 52 return; 53 } 54 55 // Original image 56 int x = 30, y = 30; 57 gc1.drawImage(image, x, y); 58 x += rect.width + 30; 59 60 Transform transform = new Transform(display); 61 62 // Note that the tranform is applied to the whole GC therefore 63 // the coordinates need to be adjusted too. 64 65 // Reflect around the y axis. 66 transform.setElements(-1, 0, 0, 1, 0 ,0); 67 gc1.setTransform(transform); 68 gc1.drawImage(image, -1*x-rect.width, y); 69 70 x = 30; y += rect.height + 30; 71 72 // Reflect around the x axis. 73 transform.setElements(1, 0, 0, -1, 0, 0); 74 gc1.setTransform(transform); 75 gc1.drawImage(image, x, -1*y-rect.height); 76 77 x += rect.width + 30; 78 79 // Reflect around the x and y axes 80 transform.setElements(-1, 0, 0, -1, 0, 0); 81 gc1.setTransform(transform); 82 gc1.drawImage(image, -1*x-rect.width, -1*y-rect.height); 83 84 x = 30; y += rect.height + 30; 85 86 // Shear in the x-direction 87 transform.setElements(1, 0, -1, 1, 0, 0); 88 gc1.setTransform(transform); 89 gc1.drawImage(image, 300, y); 90 91 // Shear in y-direction 92 transform.setElements(1, -1, 0, 1, 0, 0); 93 gc1.setTransform(transform); 94 gc1.drawImage(image, 150, 475); 95 96 // Rotate by 45 degrees 97 float cos45 = (float)Math.cos(Math.PI/4); 98 float sin45 = (float)Math.sin(Math.PI/4); 99 transform.setElements(cos45, sin45, -sin45, cos45, 0, 0); 100 gc1.setTransform(transform); 101 gc1.drawImage(image, 400, 60); 102 103 transform.dispose(); 104 }); 105 106 shell.setSize(350, 550); 107 shell.open(); 108 while (!shell.isDisposed()) { 109 if (!display.readAndDispatch()) 110 display.sleep(); 111 } 112 image.dispose(); 113 display.dispose(); 114 } 115 } 116 117 118 119 120