Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4db43e92b6be8b30750f2a5d64fc6873746ac817 (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
/*******************************************************************************
 * Copyright (c) 2006, 2016 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.SWT;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;

/**
 * Miscellaneous tab that demonstrates emerging colors from layering other
 * colors.
 */
public class RGBTab extends AnimatedGraphicsTab {

	int translateX, translateY;
	float diagTranslateX1, diagTranslateX2, diagTranslateY1, diagTranslateY2;

/**
 * Constructor
 * @param example A GraphicsExample
 */
public RGBTab(GraphicsExample example) {
	super(example);
	translateX = translateY = 0;
	diagTranslateX1 = diagTranslateX2 = diagTranslateY1 = diagTranslateY2 = 0;
}

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

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

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

@Override
public void next(int width, int height) {

	float h = height;
	float w = width;

	translateX = (translateX+3)%width;
	translateY = (translateY+5)%height;

	diagTranslateX1 = (diagTranslateX1+6)%width;
	diagTranslateY1 = diagTranslateX1*(h/w);

	diagTranslateX2 = (diagTranslateX2+8)%width;
	diagTranslateY2 = -diagTranslateX2*(h/w) + h;
}

@Override
public void paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();

	// horizontal rectangle
	Transform transform = new Transform(device);
	transform.translate(0, translateY);
	gc.setTransform(transform);
	transform.dispose();

	Path path = new Path(device);
	path.addRectangle(0, 0, width, 50);
	Pattern pattern = new Pattern(device, 0, 0, width, 50,
				device.getSystemColor(SWT.COLOR_BLUE), 0x7f,
				device.getSystemColor(SWT.COLOR_RED), 0x7f);
	gc.setBackgroundPattern(pattern);
	gc.fillPath(path);
	gc.drawPath(path);
	path.dispose();

	// vertical rectangle
	transform = new Transform(device);
	transform.translate(translateX, 0);
	gc.setTransform(transform);
	transform.dispose();

	path = new Path(device);
	path.addRectangle(0, 0, 50, height);
	pattern.dispose();
	pattern = new Pattern(device, 0, 0, 50, height,
				device.getSystemColor(SWT.COLOR_DARK_CYAN), 0x7f,
				device.getSystemColor(SWT.COLOR_WHITE), 0x7f);
	gc.setBackgroundPattern(pattern);
	gc.fillPath(path);
	gc.drawPath(path);
	path.dispose();

	// diagonal rectangle from bottom right corner
	Rectangle rect = new Rectangle(0, 0, 50, height);
	transform = new Transform(device);
	transform.translate(width-diagTranslateX1, (height/2)-diagTranslateY1);

	// rotate on center of rectangle
	transform.translate(rect.width/2, rect.height/2);
	transform.rotate(45);
	transform.translate(-rect.width/2, -rect.height/2);
	gc.setTransform(transform);
	transform.dispose();

	path = new Path(device);
	path.addRectangle(rect.x, rect.y, rect.width, rect.height);
	pattern.dispose();
	pattern = new Pattern(device, rect.x, rect.y, rect.width, rect.height,
			device.getSystemColor(SWT.COLOR_DARK_GREEN), 0x7f,
			device.getSystemColor(SWT.COLOR_DARK_MAGENTA), 0x7f);
	gc.setBackgroundPattern(pattern);
	gc.fillPath(path);
	gc.drawPath(path);
	path.dispose();

	// diagonal rectangle from top right corner
	transform = new Transform(device);
	transform.translate(width-diagTranslateX2, (height/2)-diagTranslateY2);

	// rotate on center of rectangle
	transform.translate(rect.width/2, rect.height/2);
	transform.rotate(-45);
	transform.translate(-rect.width/2, -rect.height/2);
	gc.setTransform(transform);
	transform.dispose();

	path = new Path(device);
	path.addRectangle(rect.x, rect.y, rect.width, rect.height);
	pattern.dispose();
	pattern = new Pattern(device, rect.x, rect.y, rect.width, rect.height,
			device.getSystemColor(SWT.COLOR_DARK_RED), 0x7f,
			device.getSystemColor(SWT.COLOR_YELLOW), 0x7f);
	gc.setBackgroundPattern(pattern);
	gc.fillPath(path);
	gc.drawPath(path);
	pattern.dispose();
	path.dispose();
}

}

Back to the top