28
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * D Port:
|
|
11 * Thomas Demmer <t_demmer AT web DOT de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.snippets.Snippet294;
|
|
14
|
|
15 /* Port OK */
|
|
16 import org.eclipse.swt.SWT;
|
|
17 import org.eclipse.swt.graphics.Image;
|
|
18 import org.eclipse.swt.graphics.Region;
|
|
19 import org.eclipse.swt.layout.FillLayout;
|
|
20 import org.eclipse.swt.widgets.Button;
|
|
21 import org.eclipse.swt.widgets.Display;
|
|
22 import org.eclipse.swt.widgets.Event;
|
|
23 import org.eclipse.swt.widgets.Listener;
|
|
24 import org.eclipse.swt.widgets.Shell;
|
|
25
|
|
26 import java.lang.all;
|
|
27 import tango.util.Convert;
|
|
28
|
|
29 void main(String[] args){
|
|
30 Snippet294.main(args);
|
|
31 }
|
|
32
|
|
33 /*
|
|
34 * Region on a control: create a non-rectangular button
|
|
35 *
|
|
36 * For a list of all SWT example snippets see
|
|
37 * http://www.eclipse.org/swt/snippets/
|
|
38 *
|
|
39 * @since 3.4
|
|
40 */
|
|
41
|
|
42 public class Snippet294 {
|
|
43
|
|
44 static int[] circle(int r, int offsetX, int offsetY) {
|
|
45 int[] polygon = new int[8 * r + 4];
|
|
46 // x^2 + y^2 = r^2
|
|
47 for (int i = 0; i < 2 * r + 1; i++) {
|
|
48 int x = i - r;
|
|
49 int y = cast(int)Math.sqrt(cast(real)(r*r - x*x));
|
|
50 polygon[2*i] = offsetX + x;
|
|
51 polygon[2*i+1] = offsetY + y;
|
|
52 polygon[8*r - 2*i - 2] = offsetX + x;
|
|
53 polygon[8*r - 2*i - 1] = offsetY - y;
|
|
54 }
|
|
55 return polygon;
|
|
56 }
|
|
57
|
|
58 public static void main(String[] args) {
|
|
59 Display display = new Display();
|
|
60 Shell shell = new Shell(display);
|
|
61 shell.setText("Regions on a Control");
|
|
62 shell.setLayout(new FillLayout());
|
|
63 shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_RED));
|
|
64
|
|
65 Button b2 = new Button(shell, SWT.PUSH);
|
|
66 b2.setText("Button with Regions");
|
|
67
|
|
68 // define a region that looks like a circle with two holes in ot
|
|
69 Region region = new Region();
|
|
70 region.add(circle(67, 87, 77));
|
|
71 region.subtract(circle(20, 87, 47));
|
|
72 region.subtract(circle(20, 87, 113));
|
|
73
|
|
74 // define the shape of the button using setRegion
|
|
75 b2.setRegion(region);
|
|
76 b2.setLocation(100,50);
|
|
77
|
|
78 b2.addListener(SWT.Selection, new class() Listener {
|
|
79 public void handleEvent(Event e) {
|
|
80 shell.close();
|
|
81 }
|
|
82 });
|
|
83
|
|
84 shell.setSize(200,200);
|
|
85 shell.open();
|
|
86
|
|
87 while (!shell.isDisposed()) {
|
|
88 if (!display.readAndDispatch())
|
|
89 display.sleep();
|
|
90 }
|
|
91 region.dispose();
|
|
92 display.dispose();
|
|
93 }
|
|
94 }
|