summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/PathClippingAnimTab.java
blob: 343c4ef90b653418829064a3b8b1079d307127e8 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.swt.examples.graphics;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This is another tab that demonstrates the use of a path clipping.
 */
public class PathClippingAnimTab extends AnimatedGraphicsTab {

	private Button colorButton;
	private GraphicsBackground background;
	private Menu menu;
	private int rectWidth = 300;
	private int rectHeight = 300;
	private int incWidth = 5;
	private int incHeight = 5;
	private boolean vertical = false;
	private int angle;

	public PathClippingAnimTab(GraphicsExample example) {
		super(example);
	}

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

	public String getText() {
		return GraphicsExample.getResourceString("AnimPathClipping"); //$NON-NLS-1$
	}
	
	public String getDescription() {
		return GraphicsExample.getResourceString("AnimPathClippingDesc"); //$NON-NLS-1$
	}

	public void dispose() {
		if (menu != null) {
			menu.dispose();
			menu = null;
		}
	}

	/**
	 * Creates the widgets used to control the drawing.
	 */
	public void createControlPanel(Composite parent) {
		super.createControlPanel(parent);

		// color menu
		ColorMenu cm = new ColorMenu();
		cm.setPatternItems(example.checkAdvancedGraphics());
		menu = cm.createMenu(parent.getParent(), new ColorListener() {
			public void setColor(GraphicsBackground gb) {
				background = gb;
				colorButton.setImage(gb.getThumbNail());
				example.redraw();
			}
		});

		// initialize the background to the 5th item in the menu (blue)
		background = (GraphicsBackground) menu.getItem(4).getData();

		// color button
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout(2, false));

		colorButton = new Button(comp, SWT.PUSH);
		colorButton.setText(GraphicsExample.getResourceString("Color")); //$NON-NLS-1$
		colorButton.setImage(background.getThumbNail());
		colorButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				final Button button = (Button) event.widget;
				final Composite parent = button.getParent();
				Rectangle bounds = button.getBounds();
				Point point = parent.toDisplay(new Point(bounds.x, bounds.y));
				menu.setLocation(point.x, point.y + bounds.height);
				menu.setVisible(true);
			}
		});
	}

	public void next(int width, int height) {
		angle = (angle + 5)%360;
		if (vertical) {
			if (rectHeight <= 0) {
				incHeight = -incHeight;
			}
			if (rectHeight >= height) {
				incHeight = -incHeight;
				vertical = false;
			}
			rectHeight = rectHeight + incHeight;
		} else {
			if (rectWidth <= 0) {
				incWidth = -incWidth;
			}
			if (rectWidth >= width) {
				incWidth = -incWidth;
				vertical = true;
			}
			rectWidth = rectWidth + incWidth;
		}
	}

	public void paint(GC gc, int width, int height) {
		if (!example.checkAdvancedGraphics()) return;
		Device device = gc.getDevice();
		
		// top triangle
		Path path = new Path(device);
		path.moveTo(width/2, 0);
		path.lineTo(width/2+100, 173);
		path.lineTo(width/2-100, 173);
		path.lineTo(width/2, 0);
		
		// bottom triangle
		Path path2 = new Path(device);
		path2.moveTo(width/2, height);
		path2.lineTo(width/2+100, height-173);
		path2.lineTo(width/2-100, height-173);
		path2.lineTo(width/2, height);
		
		// left triangle
		Path path3 = new Path(device);
		path3.moveTo(0, height/2);
		path3.lineTo(173, height/2-100);
		path3.lineTo(173, height/2+100);
		path3.lineTo(0, height/2);
		
		// right triangle
		Path path4 = new Path(device);
		path4.moveTo(width, height/2);
		path4.lineTo(width-173, height/2-100);
		path4.lineTo(width-173, height/2+100);
		path4.lineTo(width, height/2);
		
		// circle
		Path path5 = new Path(device);
		path5.moveTo((width-200)/2, (height-200)/2);
		path5.addArc((width-200)/2, (height-200)/2, 200, 200, 0, 360);
		
		// top rectangle
		Path path6 = new Path(device);
		path6.addRectangle((width-40)/2, 175, 40, ((height-200)/2)-177);
		
		// bottom rectangle
		Path path7 = new Path(device);
		path7.addRectangle((width-40)/2, ((height-200)/2)+202, 40, (height-175)-(((height-200)/2)+202));
		
		// left rectangle
		Path path8 = new Path(device);
		path8.addRectangle(175, (height-40)/2, ((width-200)/2)-177, 40);
		
		// right rectangle
		Path path9 = new Path(device);
		path9.addRectangle((width-200)/2+202, (height-40)/2, (width-175)-((width-200)/2+202), 40);
		
		path.addPath(path2);
		path.addPath(path3);
		path.addPath(path4);
		path.addPath(path5);
		path.addPath(path6);
		path.addPath(path7);
		path.addPath(path8);
		path.addPath(path9);
		gc.setClipping(path);

		Pattern pattern = null;
		if (background.getBgColor1() != null) {
			gc.setBackground(background.getBgColor1());
		} else if (background.getBgImage() != null) {
			pattern = new Pattern(device, background.getBgImage());
			gc.setBackgroundPattern(pattern);
		}

		gc.setLineWidth(2);
		gc.fillRectangle((width-rectWidth)/2, (height-rectHeight)/2, rectWidth, rectHeight);
		gc.drawPath(path);
		
		if (pattern != null)
			pattern.dispose();
		
		path9.dispose();
		path8.dispose();
		path7.dispose();
		path6.dispose();
		path5.dispose();
		path4.dispose();
		path3.dispose();
		path2.dispose();
		path.dispose();
	}
}