Skip to main content
summaryrefslogtreecommitdiffstats
blob: deaaaed3156c6f362a898efafc54005c8f1e3d1b (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
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
/*******************************************************************************
 * 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 tab demonstrates the use of paths. It allows the user to see the
 * differences between filling, drawing and closing paths.
 */
public class PathTab extends GraphicsTab {

	Button colorButton, fillButton, drawButton, closeButton;
	GraphicsBackground fillColor;
	Menu menu;

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

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

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

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

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

public void createControlPanel(Composite parent) {
	
	Composite comp;

	// create draw button
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());
	
	drawButton = new Button(comp, SWT.TOGGLE);
	drawButton.setText(GraphicsExample.getResourceString("DrawPath")); //$NON-NLS-1$
	drawButton.addListener(SWT.Selection, new Listener() { 
		public void handleEvent(Event event) {
			example.redraw();
		}
	});
	drawButton.setSelection(true);
	
	// create fill button
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());
	
	fillButton = new Button(comp, SWT.TOGGLE);
	fillButton.setText(GraphicsExample.getResourceString("FillPath")); //$NON-NLS-1$
	fillButton.addListener(SWT.Selection, new Listener() { 
		public void handleEvent(Event event) {
			example.redraw();
		}
	});
	
	// create close button
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());
	
	closeButton = new Button(comp, SWT.TOGGLE);
	closeButton.setText(GraphicsExample.getResourceString("ClosePath")); //$NON-NLS-1$
	closeButton.addListener(SWT.Selection, new Listener() { 
		public void handleEvent(Event event) {
			example.redraw();
		}
	});
		
	// create color button
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());
	
	ColorMenu cm = new ColorMenu();
	cm.setPatternItems(example.checkAdvancedGraphics());
	menu = cm.createMenu(parent.getParent(), new ColorListener() {
		public void setColor(GraphicsBackground gb) {
			fillColor = gb;		
			colorButton.setImage(gb.getThumbNail());
			example.redraw();
		}
	});

	// initialize the foreground to the 5th item in the menu (green)
	fillColor = (GraphicsBackground)menu.getItem(3).getData();
	
	// color button
	colorButton = new Button(comp, SWT.PUSH);
	colorButton.setText(GraphicsExample.getResourceString("FillColor")); //$NON-NLS-1$
	colorButton.setImage(fillColor.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 paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();

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

	gc.setLineWidth(5);
	gc.setForeground(device.getSystemColor(SWT.COLOR_BLACK));
	
	// arc
	Path path = new Path(device);
	path.addArc((width-250)/2, (height-400)/2, 500, 400, 90, 180);
	if (closeButton.getSelection()) path.close();
	if (fillButton.getSelection()) gc.fillPath(path);
	if (drawButton.getSelection()) gc.drawPath(path);
	path.dispose();
	
	// shape on left
	Transform transform = new Transform(device);
	transform.translate((width-250)/4, height/2-150);
	gc.setTransform(transform);
	transform.dispose();
	path = new Path(device);
	path.cubicTo(-150, 100, 150, 200, 0, 300);
	if (closeButton.getSelection()) path.close();
	if (fillButton.getSelection()) gc.fillPath(path);
	if (drawButton.getSelection()) gc.drawPath(path);
	path.dispose();
	gc.setTransform(null);
	
	// shape on right
	path = new Path(device);
	path.moveTo(3*(width-250)/4 - 25 + 250, height/2);
	path.lineTo(3*(width-250)/4 + 50 + 250, height/2 - 200);
	path.lineTo(3*(width-250)/4 + 50 + 250, height/2 + 50);
	path.lineTo(3*(width-250)/4 - 25 + 250, height/2 + 150);
	path.lineTo(3*(width-250)/4 + 25 + 250, height/2 + 50);
	if (closeButton.getSelection()) path.close();
	if (fillButton.getSelection()) gc.fillPath(path);
	if (drawButton.getSelection()) gc.drawPath(path);
	path.dispose();

	if (pattern != null) pattern.dispose();
}
}


Back to the top