summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/ImageTransformTab.java
blob: f19774eed1572745734b2173a2502b789b28ed85 (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
/*******************************************************************************
 * 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 transformations, such as scaling, rotation, and
 * invert.  It allows the user to specify values for scaling and rotation.
 */
public class ImageTransformTab extends GraphicsTab {

	private Spinner rotateSpinner, scaleSpinnerX, scaleSpinnerY;
	private Button invertButton;

/**
 * Constructor
 * @param example A GraphicsExample
 */
public ImageTransformTab(GraphicsExample example) {
	super(example);
}

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

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

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

/**
 * This method creates the controls specific to the tab. The call to the
 * createControlPanel method in the super class create the controls that are
 * defined in the super class.
 * 
 * @param parent The parent composite 
 */
public void createControlPanel(Composite parent) {
	
	Composite comp;
	GridLayout gridLayout = new GridLayout(2, false);
	
	// create spinner for the rotation angle
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(gridLayout);
 
	new Label(comp, SWT.CENTER).setText(GraphicsExample.getResourceString("Rotate")); //$NON-NLS-1$
	rotateSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP);
	rotateSpinner.setSelection(0);
	rotateSpinner.setMinimum(0);
	rotateSpinner.setMaximum(360);
	rotateSpinner.setIncrement(10);
	rotateSpinner.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {	
				example.redraw();
		}
	});
	
	// create a slider for scaling along the x axis
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(gridLayout);
	
	new Label(comp, SWT.CENTER).setText(GraphicsExample.getResourceString("xscale")); //$NON-NLS-1$
	scaleSpinnerX = new Spinner(comp, SWT.BORDER | SWT.WRAP);
	scaleSpinnerX.setDigits(2);
	scaleSpinnerX.setMinimum(1);
	scaleSpinnerX.setMaximum(500);
	scaleSpinnerX.setSelection(75);
	scaleSpinnerX.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {	
				example.redraw();
		}
	});
	
	// create a slider for scaling along the y axis
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(gridLayout);

	new Label(comp, SWT.CENTER).setText(GraphicsExample.getResourceString("yscale")); //$NON-NLS-1$
	scaleSpinnerY = new Spinner(comp, SWT.BORDER | SWT.WRAP);
	scaleSpinnerY.setDigits(2);
	scaleSpinnerY.setMinimum(1);
	scaleSpinnerY.setMaximum(500);
	scaleSpinnerY.setSelection(75);
	scaleSpinnerY.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {	
				example.redraw();
		}
	});
	
	// create a button for "invert"
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(new GridLayout());
	invertButton = new Button(comp, SWT.TOGGLE);
	invertButton.setText(GraphicsExample.getResourceString("Invert")); //$NON-NLS-1$
	invertButton.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event event) {	
				example.redraw();
		}
	});
}

/* (non-Javadoc)
 * @see org.eclipse.swt.examples.graphics.GraphicsTab#paint(org.eclipse.swt.graphics.GC, int, int)
 */
public void paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();

	Image image = GraphicsExample.loadImage(device, GraphicsExample.class, "ace_club.jpg"); 

	Transform transform = new Transform(device);
	
	// scale image
	transform.scale(scaleSpinnerX.getSelection()/100f, scaleSpinnerY.getSelection()/100f);
	
	// rotate on center of image
	Rectangle rect = image.getBounds();
	transform.translate(rect.width/2, rect.height/2);
	transform.rotate(rotateSpinner.getSelection());
	transform.translate(-rect.width/2, -rect.height/2);
	
	if(invertButton.getSelection()){
		transform.invert();
	}
	
	gc.setTransform(transform);
	
	gc.drawImage(image, 0, 0);
	gc.drawRoundRectangle(0, 0, image.getBounds().width, 
								image.getBounds().height, 22, 22);
	transform.dispose();
	image.dispose();
}

}