1 | /*
|
---|
2 | * SWT003_02.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 |
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.Iterator;
|
---|
14 |
|
---|
15 | import org.eclipse.swt.*;
|
---|
16 | import org.eclipse.swt.widgets.*;
|
---|
17 | import org.eclipse.swt.events.*;
|
---|
18 | import org.eclipse.swt.graphics.*;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * This exaple tests multicolor capabilities.
|
---|
22 | * It draws three gradient strips (from black to each of the base colors: red,
|
---|
23 | * green and blue) and the outlined rectangle using the control's foreground
|
---|
24 | * color.
|
---|
25 | *
|
---|
26 | * The first attempt to close the window changes control's background and
|
---|
27 | * foreground colors to see the effect.
|
---|
28 | *
|
---|
29 | * The examle also checks the GC color setter/getter methods.
|
---|
30 | */
|
---|
31 |
|
---|
32 | public class SWT003_02 extends SWTTestCase {
|
---|
33 |
|
---|
34 | static {
|
---|
35 | STEP= "003";
|
---|
36 | TEST = "02";
|
---|
37 | DESC = "Colors";
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static void main (String [] args) {
|
---|
41 | go (new SWT003_02 ());
|
---|
42 | }
|
---|
43 |
|
---|
44 | Shell createTopShell (Display display) {
|
---|
45 |
|
---|
46 | final Shell shell = new Shell (display,
|
---|
47 | SWT.SHELL_TRIM | SWT.NO_REDRAW_RESIZE );
|
---|
48 |
|
---|
49 | final HashMap palette = new HashMap();
|
---|
50 |
|
---|
51 | final Color bg = new Color (display, 254, 254, 180);
|
---|
52 | final Color fg = new Color (display, 255, 100, 100);
|
---|
53 | palette.put (bg.getRGB (), bg);
|
---|
54 | palette.put (fg.getRGB (), fg);
|
---|
55 |
|
---|
56 | shell.addDisposeListener (new DisposeListener () {
|
---|
57 | public void widgetDisposed (DisposeEvent e)
|
---|
58 | {
|
---|
59 | Iterator i = palette.values ().iterator ();
|
---|
60 | while (i.hasNext ())
|
---|
61 | ((Color)i.next ()).dispose();
|
---|
62 | }
|
---|
63 | });
|
---|
64 |
|
---|
65 | shell.addShellListener (new ShellAdapter () {
|
---|
66 | int closes = 0;
|
---|
67 | public void shellClosed (ShellEvent e) {
|
---|
68 | closes++;
|
---|
69 | e.doit = false;
|
---|
70 | switch (closes) {
|
---|
71 | case 1:
|
---|
72 | saySubObjective ("\nChanging shell's background and foreground");
|
---|
73 | say ("Foreground trying to be set: " + fg);
|
---|
74 | say ("Background trying to be set: " + bg);
|
---|
75 | shell.setBackground (bg);
|
---|
76 | shell.setForeground (fg);
|
---|
77 | break;
|
---|
78 | default: e.doit = true;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | });
|
---|
82 |
|
---|
83 | shell.addPaintListener(new PaintListener () {
|
---|
84 | public void paintControl(PaintEvent event){
|
---|
85 | GC gc = event.gc;
|
---|
86 | say ("GC foreground actually set: " + gc.getForeground ());
|
---|
87 | say ("GC background actually set: " + gc.getBackground ());
|
---|
88 | gc.drawRectangle (10, 100, 255, 20);
|
---|
89 | drawGradientStrip (gc, 10, 70, 20, 2, palette);
|
---|
90 | drawGradientStrip (gc, 10, 10, 20, 1, palette);
|
---|
91 | drawGradientStrip (gc, 10, 40, 20, 0, palette);
|
---|
92 | }
|
---|
93 | });
|
---|
94 |
|
---|
95 | System.out.println("Shell default foreground: "+shell.getForeground ());
|
---|
96 | System.out.println("Shell default background: "+shell.getBackground ());
|
---|
97 |
|
---|
98 | Rectangle dr = display.getClientArea ();
|
---|
99 | Rectangle sr = new Rectangle (0, 0, 300, 200); //shell.getBounds ();
|
---|
100 | sr.x = dr.x + (dr.width-sr.width)/2;
|
---|
101 | sr.y = dr.y + (dr.height-sr.height)/2;
|
---|
102 | shell.setBounds (sr);
|
---|
103 | return shell;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void drawGradientStrip (GC gc, int x0, int y0, int height, int mode, HashMap palette)
|
---|
107 | {
|
---|
108 | Color color;
|
---|
109 | RGB rgb;
|
---|
110 |
|
---|
111 | for (int i = 0; i < 256; i++) {
|
---|
112 | switch (mode) {
|
---|
113 | case 0: rgb = new RGB (i, 0, 0); break;
|
---|
114 | case 1: rgb = new RGB (0, i, 0); break;
|
---|
115 | default: rgb = new RGB (0, 0, i); break;
|
---|
116 | }
|
---|
117 | color = (Color)palette.get (rgb);
|
---|
118 | if (color == null) {
|
---|
119 | color = new Color (null, rgb);
|
---|
120 | palette.put (rgb, color);
|
---|
121 | }
|
---|
122 | gc.setForeground (color);
|
---|
123 | gc.drawRectangle (x0 + i, y0, 0, height);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|