Mercurial > projects > dwt-samples
comparison snippets/button/Snippet294.d @ 117:8cdaac0dc743
Added more snippets from TomD
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 12 Jul 2008 20:09:06 +0200 |
parents | 0de3dab4d6e1 |
children |
comparison
equal
deleted
inserted
replaced
116:f53c6274734f | 117:8cdaac0dc743 |
---|---|
25 | 25 |
26 import dwt.dwthelper.utils; | 26 import dwt.dwthelper.utils; |
27 import tango.util.Convert; | 27 import tango.util.Convert; |
28 | 28 |
29 void main(String[] args){ | 29 void main(String[] args){ |
30 Snippet294.main(args); | 30 Snippet294.main(args); |
31 } | 31 } |
32 | 32 |
33 /* | 33 /* |
34 * Region on a control: create a non-rectangular button | 34 * Region on a control: create a non-rectangular button |
35 * | 35 * |
39 * @since 3.4 | 39 * @since 3.4 |
40 */ | 40 */ |
41 | 41 |
42 public class Snippet294 { | 42 public class Snippet294 { |
43 | 43 |
44 static int[] circle(int r, int offsetX, int offsetY) { | 44 static int[] circle(int r, int offsetX, int offsetY) { |
45 int[] polygon = new int[8 * r + 4]; | 45 int[] polygon = new int[8 * r + 4]; |
46 // x^2 + y^2 = r^2 | 46 // x^2 + y^2 = r^2 |
47 for (int i = 0; i < 2 * r + 1; i++) { | 47 for (int i = 0; i < 2 * r + 1; i++) { |
48 int x = i - r; | 48 int x = i - r; |
49 int y = cast(int)Math.sqrt(cast(real)(r*r - x*x)); | 49 int y = cast(int)Math.sqrt(cast(real)(r*r - x*x)); |
50 polygon[2*i] = offsetX + x; | 50 polygon[2*i] = offsetX + x; |
51 polygon[2*i+1] = offsetY + y; | 51 polygon[2*i+1] = offsetY + y; |
52 polygon[8*r - 2*i - 2] = offsetX + x; | 52 polygon[8*r - 2*i - 2] = offsetX + x; |
53 polygon[8*r - 2*i - 1] = offsetY - y; | 53 polygon[8*r - 2*i - 1] = offsetY - y; |
54 } | |
55 return polygon; | |
54 } | 56 } |
55 return polygon; | |
56 } | |
57 | 57 |
58 public static void main(String[] args) { | 58 public static void main(String[] args) { |
59 Display display = new Display(); | 59 Display display = new Display(); |
60 Shell shell = new Shell(display); | 60 Shell shell = new Shell(display); |
61 shell.setText("Regions on a Control"); | 61 shell.setText("Regions on a Control"); |
62 shell.setLayout(new FillLayout()); | 62 shell.setLayout(new FillLayout()); |
63 shell.setBackground(display.getSystemColor(DWT.COLOR_DARK_RED)); | 63 shell.setBackground(display.getSystemColor(DWT.COLOR_DARK_RED)); |
64 | 64 |
65 Button b2 = new Button(shell, DWT.PUSH); | 65 Button b2 = new Button(shell, DWT.PUSH); |
66 b2.setText("Button with Regions"); | 66 b2.setText("Button with Regions"); |
67 | 67 |
68 // define a region that looks like a circle with two holes in ot | 68 // define a region that looks like a circle with two holes in ot |
69 Region region = new Region(); | 69 Region region = new Region(); |
70 region.add(circle(67, 87, 77)); | 70 region.add(circle(67, 87, 77)); |
71 region.subtract(circle(20, 87, 47)); | 71 region.subtract(circle(20, 87, 47)); |
72 region.subtract(circle(20, 87, 113)); | 72 region.subtract(circle(20, 87, 113)); |
73 | 73 |
74 // define the shape of the button using setRegion | 74 // define the shape of the button using setRegion |
75 b2.setRegion(region); | 75 b2.setRegion(region); |
76 b2.setLocation(100,50); | 76 b2.setLocation(100,50); |
77 | 77 |
78 b2.addListener(DWT.Selection, new class() Listener { | 78 b2.addListener(DWT.Selection, new class() Listener { |
79 public void handleEvent(Event e) { | 79 public void handleEvent(Event e) { |
80 shell.close(); | 80 shell.close(); |
81 } | 81 } |
82 }); | 82 }); |
83 | 83 |
84 shell.setSize(200,200); | 84 shell.setSize(200,200); |
85 shell.open(); | 85 shell.open(); |
86 | 86 |
87 while (!shell.isDisposed()) { | 87 while (!shell.isDisposed()) { |
88 if (!display.readAndDispatch()) | 88 if (!display.readAndDispatch()) |
89 display.sleep(); | 89 display.sleep(); |
90 } | |
91 region.dispose(); | |
92 display.dispose(); | |
90 } | 93 } |
91 region.dispose(); | |
92 display.dispose(); | |
93 } | |
94 } | 94 } |