78
|
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 snippets.gc.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 dwt.DWT;
|
|
22 import dwt.events.PaintEvent;
|
|
23 import dwt.events.PaintListener;
|
|
24 import dwt.graphics.GC;
|
|
25 import dwt.graphics.Transform;
|
|
26 import dwt.graphics.Font;
|
|
27 import dwt.graphics.Rectangle;
|
|
28 import dwt.graphics.Image;
|
|
29 import dwt.layout.FillLayout;
|
|
30 import dwt.widgets.Display;
|
|
31 import dwt.widgets.Shell;
|
|
32 import dwt.widgets.Canvas;
|
|
33
|
|
34 import Math=tango.math.Math;
|
|
35
|
|
36 void main() {
|
|
37 Display display = new Display();
|
|
38
|
|
39 Image image = new Image(display, 110, 60);
|
|
40 GC gc = new GC(image);
|
|
41 Font font = new Font(display, "Times", 30., DWT.BOLD);
|
|
42 gc.setFont(font);
|
|
43 gc.setBackground(display.getSystemColor(DWT.COLOR_RED));
|
|
44 gc.fillRectangle(0, 0, 110, 60);
|
|
45 gc.setForeground(display.getSystemColor(DWT.COLOR_WHITE));
|
|
46 gc.drawText("DWT", 10, 10, true);
|
|
47 font.dispose();
|
|
48 gc.dispose();
|
|
49
|
|
50 Rectangle rect = image.getBounds();
|
|
51 Shell shell = new Shell(display);
|
|
52 shell.setText("Matrix Tranformations");
|
|
53 shell.setLayout(new FillLayout());
|
|
54 Canvas canvas = new Canvas(shell, DWT.DOUBLE_BUFFERED);
|
|
55 canvas.addPaintListener(new class() PaintListener {
|
|
56 public void paintControl(PaintEvent e) {
|
|
57 GC gc = e.gc;
|
|
58 gc.setAdvanced(true);
|
|
59 if (!gc.getAdvanced()){
|
|
60 gc.drawText("Advanced graphics not supported", 30, 30, true);
|
|
61 return;
|
|
62 }
|
|
63
|
|
64 // Original image
|
|
65 int x = 30, y = 30;
|
|
66 gc.drawImage(image, x, y);
|
|
67 x += rect.width + 30;
|
|
68
|
|
69 Transform transform = new Transform(display);
|
|
70
|
|
71 // Note that the tranform is applied to the whole GC therefore
|
|
72 // the coordinates need to be adjusted too.
|
|
73
|
|
74 // Reflect around the y axis.
|
|
75 transform.setElements(-1, 0, 0, 1, 0 ,0);
|
|
76 gc.setTransform(transform);
|
|
77 gc.drawImage(image, -1*x-rect.width, y);
|
|
78
|
|
79 x = 30; y += rect.height + 30;
|
|
80
|
|
81 // Reflect around the x axis.
|
|
82 transform.setElements(1, 0, 0, -1, 0, 0);
|
|
83 gc.setTransform(transform);
|
|
84 gc.drawImage(image, x, -1*y-rect.height);
|
|
85
|
|
86 x += rect.width + 30;
|
|
87
|
|
88 // Reflect around the x and y axes
|
|
89 transform.setElements(-1, 0, 0, -1, 0, 0);
|
|
90 gc.setTransform(transform);
|
|
91 gc.drawImage(image, -1*x-rect.width, -1*y-rect.height);
|
|
92
|
|
93 x = 30; y += rect.height + 30;
|
|
94
|
|
95 // Shear in the x-direction
|
|
96 transform.setElements(1, 0, -1, 1, 0, 0);
|
|
97 gc.setTransform(transform);
|
|
98 gc.drawImage(image, 300, y);
|
|
99
|
|
100 // Shear in y-direction
|
|
101 transform.setElements(1, -1, 0, 1, 0, 0);
|
|
102 gc.setTransform(transform);
|
|
103 gc.drawImage(image, 150, 475);
|
|
104
|
|
105 // Rotate by 45 degrees
|
|
106 float cos45 = Math.cos(45);
|
|
107 float sin45 = Math.sin(45);
|
|
108 transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
|
|
109 gc.setTransform(transform);
|
|
110 gc.drawImage(image, 350, 100);
|
|
111
|
|
112 transform.dispose();
|
|
113 }
|
|
114 });
|
|
115
|
|
116 shell.setSize(350, 550);
|
|
117 shell.open();
|
|
118 while (!shell.isDisposed()) {
|
|
119 if (!display.readAndDispatch())
|
|
120 display.sleep();
|
|
121 }
|
|
122 image.dispose();
|
|
123 display.dispose();
|
|
124 }
|
|
125
|
|
126
|
|
127
|
|
128
|
|
129
|