28
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Bill Baxter <bill@billbaxter.com>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.snippets.Snippet207;
|
|
14
|
|
15 /*
|
|
16 * Use transformation matrices to reflect, rotate and shear images
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21 import org.eclipse.swt.SWT;
|
|
22 import org.eclipse.swt.events.PaintEvent;
|
|
23 import org.eclipse.swt.events.PaintListener;
|
|
24 import org.eclipse.swt.graphics.GC;
|
|
25 import org.eclipse.swt.graphics.Transform;
|
|
26 import org.eclipse.swt.graphics.Font;
|
|
27 import org.eclipse.swt.graphics.Rectangle;
|
|
28 import org.eclipse.swt.graphics.Image;
|
|
29 import org.eclipse.swt.layout.FillLayout;
|
|
30 import org.eclipse.swt.widgets.Display;
|
|
31 import org.eclipse.swt.widgets.Shell;
|
|
32 import org.eclipse.swt.widgets.Canvas;
|
|
33
|
112
|
34 version(Tango){
|
|
35 import Math=tango.math.Math;
|
|
36 } else { // Phobos
|
|
37 import Math=std.math;
|
|
38 }
|
28
|
39
|
|
40 void main() {
|
|
41 Display display = new Display();
|
|
42
|
|
43 Image image = new Image(display, 110, 60);
|
|
44 GC gc = new GC(image);
|
|
45 Font font = new Font(display, "Times", 30., SWT.BOLD);
|
|
46 gc.setFont(font);
|
|
47 gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
|
|
48 gc.fillRectangle(0, 0, 110, 60);
|
|
49 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
|
|
50 gc.drawText("SWT", 10, 10, true);
|
|
51 font.dispose();
|
|
52 gc.dispose();
|
|
53
|
|
54 Rectangle rect = image.getBounds();
|
|
55 Shell shell = new Shell(display);
|
|
56 shell.setText("Matrix Tranformations");
|
|
57 shell.setLayout(new FillLayout());
|
|
58 Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
|
|
59 canvas.addPaintListener(new class() PaintListener {
|
|
60 public void paintControl(PaintEvent e) {
|
|
61 GC gc = e.gc;
|
|
62 gc.setAdvanced(true);
|
|
63 if (!gc.getAdvanced()){
|
|
64 gc.drawText("Advanced graphics not supported", 30, 30, true);
|
|
65 return;
|
|
66 }
|
|
67
|
|
68 // Original image
|
|
69 int x = 30, y = 30;
|
|
70 gc.drawImage(image, x, y);
|
|
71 x += rect.width + 30;
|
|
72
|
|
73 Transform transform = new Transform(display);
|
|
74
|
|
75 // Note that the tranform is applied to the whole GC therefore
|
|
76 // the coordinates need to be adjusted too.
|
|
77
|
|
78 // Reflect around the y axis.
|
|
79 transform.setElements(-1, 0, 0, 1, 0 ,0);
|
|
80 gc.setTransform(transform);
|
|
81 gc.drawImage(image, -1*x-rect.width, y);
|
|
82
|
|
83 x = 30; y += rect.height + 30;
|
|
84
|
|
85 // Reflect around the x axis.
|
|
86 transform.setElements(1, 0, 0, -1, 0, 0);
|
|
87 gc.setTransform(transform);
|
|
88 gc.drawImage(image, x, -1*y-rect.height);
|
|
89
|
|
90 x += rect.width + 30;
|
|
91
|
|
92 // Reflect around the x and y axes
|
|
93 transform.setElements(-1, 0, 0, -1, 0, 0);
|
|
94 gc.setTransform(transform);
|
|
95 gc.drawImage(image, -1*x-rect.width, -1*y-rect.height);
|
|
96
|
|
97 x = 30; y += rect.height + 30;
|
|
98
|
|
99 // Shear in the x-direction
|
|
100 transform.setElements(1, 0, -1, 1, 0, 0);
|
|
101 gc.setTransform(transform);
|
|
102 gc.drawImage(image, 300, y);
|
|
103
|
|
104 // Shear in y-direction
|
|
105 transform.setElements(1, -1, 0, 1, 0, 0);
|
|
106 gc.setTransform(transform);
|
|
107 gc.drawImage(image, 150, 475);
|
|
108
|
|
109 // Rotate by 45 degrees
|
|
110 float cos45 = Math.cos(45);
|
|
111 float sin45 = Math.sin(45);
|
|
112 transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
|
|
113 gc.setTransform(transform);
|
|
114 gc.drawImage(image, 350, 100);
|
|
115
|
|
116 transform.dispose();
|
|
117 }
|
|
118 });
|
|
119
|
|
120 shell.setSize(350, 550);
|
|
121 shell.open();
|
|
122 while (!shell.isDisposed()) {
|
|
123 if (!display.readAndDispatch())
|
|
124 display.sleep();
|
|
125 }
|
|
126 image.dispose();
|
|
127 display.dispose();
|
|
128 }
|
|
129
|
|
130
|
|
131
|
|
132
|
|
133
|