source: trunk/tests/SWT/java/SWT003_01.java

Last change on this file was 12, checked in by lpino, 18 years ago
  • Initial commit
File size: 10.1 KB
Line 
1/*
2 * SWT003_01.java
3 */
4
5/*
6 * Copyright (c) 2002, 2004 EclipseOS2 Team.
7 * This file is made available under the terms of the Common Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/cpl-v10.html
10 */
11
12import org.eclipse.swt.*;
13import org.eclipse.swt.widgets.*;
14import org.eclipse.swt.events.*;
15import org.eclipse.swt.graphics.*;
16
17/**
18 * This example tests the basic drawing capabilities.
19 * It draws different graphic primitives in different modes (outlined/filled,
20 * positive/negative dimensions and so on) on a grid with cells of 20
21 * pixels in size.
22 *
23 * When the window is being closed for the first time everything is redrawn
24 * with the thickness set to 3 points.
25 *
26 * When the window is being closed for the second time everything is redrawn in
27 * the XOR mode.
28 */
29
30public class SWT003_01 extends SWTTestCase {
31
32static{
33 STEP= "003";
34 TEST = "01";
35 DESC = "Simple Drawings";
36}
37
38int closes = 0;
39
40public static void main (String [] args) {
41 go (new SWT003_01 ());
42}
43
44Shell createTopShell (Display display) {
45
46 final Color red = new Color (display, 0xFF, 0, 0);
47 final Color blue = new Color (display, 0, 0, 0xFF);
48 final Color black = new Color (display, 0, 0, 0);
49 final Color grid = new Color (display, 0xA0, 0xA0, 0xA0);
50
51 final Shell shell = new Shell (display,
52 SWT.SHELL_TRIM | /*SWT.NO_REDRAW_RESIZE |*/ SWT.NO_MERGE_PAINTS);
53
54 shell.addDisposeListener (new DisposeListener () {
55 public void widgetDisposed (DisposeEvent e) {
56 grid.dispose();
57 black.dispose();
58 blue.dispose();
59 red.dispose();
60 }
61 });
62
63 shell.addShellListener (new ShellAdapter () {
64 public void shellClosed (ShellEvent e) {
65 closes++;
66 e.doit = false;
67 switch (closes) {
68 case 1:
69 saySubObjective ("\nSetting thick line width for all primitives");
70 shell.redraw ();
71 break;
72 case 2:
73 saySubObjective ("\nSetting XOR mode for all primitives");
74 shell.redraw ();
75 break;
76 default: e.doit = true;
77 }
78 }
79 });
80
81 shell.addPaintListener(new PaintListener () {
82 public void paintControl(PaintEvent event) {
83 GC gc = event.gc;
84
85 if (closes == 1)
86 gc.setLineWidth (3);
87 else if (closes == 2)
88 gc.setXORMode (true);
89
90 say ("Line style: " + gc.getLineStyle ());
91 say ("Line width: " + gc.getLineWidth ());
92 say ("XOR mode: " + gc.getXORMode ());
93
94 say ("PaintEvent: count = " + event.count);
95 say (
96 " [x = "+event.x +
97 ", y = "+event.y +
98 ", width = "+event.width +
99 ", height = "+event.height + "]");
100
101 Rectangle r = shell.getClientArea();
102
103 gc.setForeground(grid);
104 drawGrid (gc, 10, 10, r.width-20, r.height-20, 20);
105
106 gc.setForeground(blue);
107
108 gc.drawLine (0,0,10,10);
109 r.width--; r.height--;
110 gc.drawRectangle (r);
111
112 gc.setBackground (red);
113 drawCheckBoard (gc, 10, 10, false);
114 drawCheckBoard (gc, 230, 10, true);
115
116 drawStairs (gc, 10, 230, 0);
117 drawStairs (gc, 10, 250, DS_FILLED);
118 drawStairs (gc, 230, 230, DS_ROUNDED);
119 drawStairs (gc, 230, 250, DS_FILLED | DS_ROUNDED);
120
121 drawStairs (gc, 10, 230, DS_NEGATIVE);
122 drawStairs (gc, 10, 250, DS_FILLED | DS_NEGATIVE);
123 drawStairs (gc, 230, 230, DS_ROUNDED | DS_NEGATIVE);
124 drawStairs (gc, 230, 250, DS_FILLED | DS_ROUNDED | DS_NEGATIVE);
125
126 drawArcs (gc, 450, 10);
127
128 gc.drawRoundRectangle (610, 10, 39, 39, 20, 40);
129 gc.fillRoundRectangle (650, 10, 39, 39, 20, 40);
130 gc.fillRoundRectangle (610, 50, 39, 39, 40, 20);
131 gc.drawRoundRectangle (650, 50, 39, 39, 40, 20);
132
133 for (int i = 0; i < 6; i++) {
134 gc.drawArc (610, 110, 39, 39, 20+i*60, 40);
135 gc.drawArc (650, 110, 40, 40, 20+i*60, 40);
136 gc.fillArc (610, 150, 39, 39, 20+i*60, 40);
137 gc.fillArc (650, 150, 40, 40, 20+i*60, 40);
138 }
139
140 int[] points = new int [] {
141 10, 310, 110, 390, 70, 270, 30, 390, 130, 310
142 };
143 gc.drawPolyline (points);
144 for (int i = 0; i < points.length; i += 2) points[i] += 140;
145 gc.drawPolygon (points);
146 for (int i = 0; i < points.length; i += 2) points[i] += 140;
147 gc.fillPolygon (points);
148
149 for (int i = 0; i < 3; i++) {
150 switch (i) {
151 case 1: gc.setLineWidth (10); break;
152 case 2: gc.setLineWidth (0); break;
153 default:;
154 }
155 gc.setLineStyle (SWT.LINE_SOLID);
156 drawZigzag (gc, 450 + i*120, 290);
157 gc.setLineStyle (SWT.LINE_DASH);
158 drawZigzag (gc, 450 + i*120, 310);
159 gc.setLineStyle (SWT.LINE_DOT);
160 drawZigzag (gc, 450 + i*120, 330);
161 gc.setLineStyle (SWT.LINE_DASHDOT);
162 drawZigzag (gc, 450 + i*120, 350);
163 gc.setLineStyle (SWT.LINE_DASHDOTDOT);
164 drawZigzag (gc, 450 + i*120, 370);
165 }
166
167 gc.setForeground(black);
168 gc.setBackground (shell.getBackground());
169 drawStairs (gc, 10, 410, DS_FOCUS);
170 drawStairs (gc, 10, 410, DS_FOCUS | DS_NEGATIVE);
171
172 gc.setBackground (red);
173 gc.fillRectangle (230, 410, 20, 20);
174 gc.drawFocus (235, 415, 10, 10);
175
176 gc.setBackground (black);
177 gc.setForeground (red);
178 gc.fillRectangle (250, 410, 20, 20);
179 gc.drawFocus (255, 415, 10, 10);
180
181 gc.drawFocus (275, 415, 10, 10);
182
183 // the following should finally not affect the picture
184 drawStairs (gc, 10, 10, DS_FOCUS);
185 drawStairs (gc, 10, 10, DS_FOCUS);
186 }
187 });
188
189 Rectangle dr = display.getBounds ();
190 Rectangle sr = shell.getBounds ();
191
192 sr.x = (dr.width-sr.width)/2;
193 sr.y = (dr.height-sr.height)/2;
194
195 shell.setBounds (sr);
196
197 System.out.println("bounds:");
198 System.out.println(" shell.getBounds() = " + shell.getBounds ());
199 System.out.println(" shell.getClientArea() = " + shell.getClientArea ());
200 return shell;
201}
202
203void drawGrid (GC gc, int x0, int y0, int width, int height, int step)
204{
205 int i;
206 for (i = 0; i <= width/step; i++)
207 gc.drawLine (x0+i*step, y0, x0+i*step, y0+height);
208 for (i = 0; i <= height/step; i++)
209 gc.drawLine (x0, y0+i*step, x0+width, y0+i*step);
210}
211
212static final int DS_NEGATIVE = 0x01;
213static final int DS_FOCUS = 0x02;
214static final int DS_FILLED = 0x04;
215static final int DS_ROUNDED = 0x08;
216
217void drawStairs (GC gc, int x0, int y0, int style)
218{
219 int sz = 20;
220 int n;
221 for (int i = 0; i < 10; i++) {
222 if ((style & DS_NEGATIVE) != 0) n = -i;
223 else n = i;
224 if ((style & DS_FOCUS) != 0) {
225 gc.drawFocus (x0+i*sz, y0, n, n);
226 } else {
227 if ((style & DS_ROUNDED) != 0) {
228 if ((style & DS_FILLED) != 0)
229 gc.fillRoundRectangle (x0+i*sz, y0, n, n, i/2, i/2);
230 else
231 gc.drawRoundRectangle (x0+i*sz, y0, n, n, i/2, i/2);
232 } else {
233 if ((style & DS_FILLED) != 0)
234 gc.fillRectangle (x0+i*sz, y0, n, n);
235 else
236 gc.drawRectangle (x0+i*sz, y0, n, n);
237 }
238 }
239 }
240}
241
242void drawCheckBoard (GC gc, int x0, int y0, boolean rounded)
243{
244 int sz = 19, nx = 10, ny = 10, szrnd = 10;
245 int xs, ys;
246 boolean fill;
247
248 for (int y = 0; y < ny; y++)
249 for (int x = 0; x < nx; x++) {
250 xs = x0 + x * (sz+1);
251 ys = y0 + y * (sz+1);
252 fill = ((y % 2) == 0 && (x % 2) != 0) || ((y % 2) != 0 && (x % 2) == 0);
253 if (rounded) {
254 if (fill) gc.fillRoundRectangle (xs, ys, sz, sz, szrnd, szrnd);
255 else gc.drawRoundRectangle (xs, ys, sz, sz, szrnd, szrnd);
256 }
257 else {
258 if (fill) gc.fillRectangle (xs, ys, sz, sz);
259 else gc.drawRectangle (xs, ys, sz, sz);
260 }
261 }
262}
263
264void drawArcs (GC gc, int x0, int y0)
265{
266 int sz = 20;
267
268 gc.drawArc (x0+sz, y0, sz*2, sz*2, 0, 90);
269 gc.drawArc (x0, y0, sz*2, sz*2, 90, 90);
270 gc.drawArc (x0, y0+sz, sz*2, sz*2, 180, 90);
271 gc.drawArc (x0+sz, y0+sz, sz*2, sz*2, 270, 90);
272 gc.fillArc (x0+sz*5, y0, sz*2, sz*2, 90, -90);
273 gc.fillArc (x0+sz*4, y0, sz*2, sz*2, 180, -90);
274 gc.fillArc (x0+sz*4, y0+sz, sz*2, sz*2, 270, -90);
275 gc.fillArc (x0+sz*5, y0+sz, sz*2, sz*2, 360, -90);
276
277 gc.drawArc (x0+sz, y0+sz*4, sz*2, sz*4, 0, 90);
278 gc.drawArc (x0, y0+sz*4, sz*2, sz*4, 90, 90);
279 gc.drawArc (x0, y0+sz*5, sz*2, sz*4, 180, 90);
280 gc.drawArc (x0+sz, y0+sz*5, sz*2, sz*4, 270, 90);
281 gc.fillArc (x0+sz*5, y0+sz*4, sz*2, sz*4, 90, -90);
282 gc.fillArc (x0+sz*4, y0+sz*4, sz*2, sz*4, 180, -90);
283 gc.fillArc (x0+sz*4, y0+sz*5, sz*2, sz*4, 270, -90);
284 gc.fillArc (x0+sz*5, y0+sz*5, sz*2, sz*4, 360, -90);
285
286 for (int i = 0; i < 9; i++) {
287 gc.drawArc (x0+i*sz, y0+sz*11, i+1, i+1, 0, 370);
288 gc.drawArc (x0+i*sz, y0+sz*11, -(i+1), -(i+1), 0, 370);
289 gc.fillArc (x0+i*sz, y0+sz*12, i+1, i+1, 0, 370);
290 gc.fillArc (x0+i*sz, y0+sz*12, -(i+1), -(i+1), 0, 370);
291 }
292}
293
294void drawZigzag (GC gc, int x, int y) {
295 int[] points = {0, 0, 20, 10, 40, 0, 60, 10, 80, 0, 100, 10};
296 for (int i = 0; i < points.length; i += 2) {
297 points[i] += x;
298 points[i+1] += y;
299 }
300 gc.drawPolyline (points);
301}
302
303}
Note: See TracBrowser for help on using the repository browser.