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