Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c5b71711aa3b0acd775bb3422b00fd6b376615a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class StarPolyTab extends GraphicsTab {
	int[] radial;
	static final int POINTS  = 11;

	Combo fillRuleCb;

public StarPolyTab(GraphicsExample example) {
	super(example);
	radial = new int[POINTS * 2];
}

@Override
public void createControlPanel(Composite parent) {
	new Label(parent, SWT.NONE).setText(GraphicsExample.getResourceString("FillRule")); //$NON-NLS-1$
	fillRuleCb = new Combo(parent, SWT.DROP_DOWN);
	fillRuleCb.add("FILL_EVEN_ODD");
	fillRuleCb.add("FILL_WINDING");
	fillRuleCb.select(0);
	fillRuleCb.addListener(SWT.Selection, event -> example.redraw());
}

@Override
public String getCategory() {
	return GraphicsExample.getResourceString("Polygons"); //$NON-NLS-1$
}

@Override
public String getText() {
	return GraphicsExample.getResourceString("StarPolygon"); //$NON-NLS-1$
}

@Override
public String getDescription() {
	return GraphicsExample.getResourceString("StarPolygonDescription"); //$NON-NLS-1$
}

@Override
public void paint(GC gc, int width, int height) {
	int centerX = width / 2;
	int centerY = height / 2;
	int pos = 0;
	for (int i = 0; i < POINTS; ++i) {
		double r = Math.PI*2 * pos/POINTS;
		radial[i*2] = (int)((1+Math.cos(r))*centerX);
		radial[i*2+1] = (int)((1+Math.sin(r))*centerY);
		pos = (pos + POINTS/2) % POINTS;
	}
	gc.setFillRule(fillRuleCb.getSelectionIndex() != 0 ? SWT.FILL_WINDING : SWT.FILL_EVEN_ODD);
	gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_YELLOW));
	gc.fillPolygon(radial);
	gc.drawPolygon(radial);
}
}

Back to the top