1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.examples.graphics;
15 
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Label;
21 
22 public class StarPolyTab extends GraphicsTab {
23 	int[] radial;
24 	static final int POINTS  = 11;
25 
26 	Combo fillRuleCb;
27 
StarPolyTab(GraphicsExample example)28 public StarPolyTab(GraphicsExample example) {
29 	super(example);
30 	radial = new int[POINTS * 2];
31 }
32 
33 @Override
createControlPanel(Composite parent)34 public void createControlPanel(Composite parent) {
35 	new Label(parent, SWT.NONE).setText(GraphicsExample.getResourceString("FillRule")); //$NON-NLS-1$
36 	fillRuleCb = new Combo(parent, SWT.DROP_DOWN);
37 	fillRuleCb.add("FILL_EVEN_ODD");
38 	fillRuleCb.add("FILL_WINDING");
39 	fillRuleCb.select(0);
40 	fillRuleCb.addListener(SWT.Selection, event -> example.redraw());
41 }
42 
43 @Override
getCategory()44 public String getCategory() {
45 	return GraphicsExample.getResourceString("Polygons"); //$NON-NLS-1$
46 }
47 
48 @Override
getText()49 public String getText() {
50 	return GraphicsExample.getResourceString("StarPolygon"); //$NON-NLS-1$
51 }
52 
53 @Override
getDescription()54 public String getDescription() {
55 	return GraphicsExample.getResourceString("StarPolygonDescription"); //$NON-NLS-1$
56 }
57 
58 @Override
paint(GC gc, int width, int height)59 public void paint(GC gc, int width, int height) {
60 	int centerX = width / 2;
61 	int centerY = height / 2;
62 	int pos = 0;
63 	for (int i = 0; i < POINTS; ++i) {
64 		double r = Math.PI*2 * pos/POINTS;
65 		radial[i*2] = (int)((1+Math.cos(r))*centerX);
66 		radial[i*2+1] = (int)((1+Math.sin(r))*centerY);
67 		pos = (pos + POINTS/2) % POINTS;
68 	}
69 	gc.setFillRule(fillRuleCb.getSelectionIndex() != 0 ? SWT.FILL_WINDING : SWT.FILL_EVEN_ODD);
70 	gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_YELLOW));
71 	gc.fillPolygon(radial);
72 	gc.drawPolygon(radial);
73 }
74 }
75