Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 658d89cbdb87f6d36b8f341219ac227bfc679d61 (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
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
/*******************************************************************************
 * Copyright (c) 2006, 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.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * This tab shows the effects of applying various interpolation algorithms to
 * images that have been stretched or shrunk.
 */
public class InterpolationTab extends GraphicsTab {

	Combo imageCb;	// combo for selecting images

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

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

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

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

@Override
public void createControlPanel(Composite parent) {

	Composite comp;
	GridLayout gridLayout = new GridLayout(2, false);

	// create drop down combo
	comp = new Composite(parent, SWT.NONE);
	comp.setLayout(gridLayout);
	new Label(comp, SWT.CENTER).setText(GraphicsExample
				.getResourceString("Image")); //$NON-NLS-1$
	imageCb = new Combo(comp, SWT.DROP_DOWN);
	imageCb.add(GraphicsExample.getResourceString("House")); //$NON-NLS-1$
	imageCb.add(GraphicsExample.getResourceString("Question")); //$NON-NLS-1$
	imageCb.add(GraphicsExample.getResourceString("Task")); //$NON-NLS-1$
	imageCb.add(GraphicsExample.getResourceString("Font")); //$NON-NLS-1$
	imageCb.add(GraphicsExample.getResourceString("Cube")); //$NON-NLS-1$
	imageCb.add(GraphicsExample.getResourceString("SWT")); //$NON-NLS-1$
	imageCb.add(GraphicsExample.getResourceString("Ovals")); //$NON-NLS-1$
	imageCb.select(0);
	imageCb.addListener(SWT.Selection, event -> example.redraw());
}

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

	float scaleX = 10f;
	float scaleY = 10f;
	Image image = null;
	switch (imageCb.getSelectionIndex()) {

	case 0:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "home_nav.gif");
		break;
	case 1:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "help.gif");
		break;
	case 2:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "task.gif");
		break;
	case 3:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "font.gif");
		break;
	case 4:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "cube.png");
		scaleX = 0.75f;
		scaleY = 0.5f;
		break;
	case 5:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "swt.png");
		scaleX = 0.4f;
		scaleY = 0.8f;
		break;
	case 6:
		image = GraphicsExample.loadImage(device, GraphicsExample.class, "ovals.png");
		scaleX = 1.1f;
		scaleY = 0.5f;
		break;
	}

	Rectangle bounds = image.getBounds();

	// draw the original image
	gc.drawImage(image, (width-bounds.width)/2, 20);

	Font font = new Font(device, getPlatformFont(), 20, SWT.NORMAL);
	gc.setFont(font);

	// write some text below the original image
	String text = GraphicsExample.getResourceString("OriginalImg"); //$NON-NLS-1$
	Point size = gc.stringExtent(text);
	gc.drawString(text, (width-size.x)/2, 25 + bounds.height, true);

	Transform transform = new Transform(device);
	transform.translate((width - (bounds.width * scaleX + 10) * 4) / 2, 25 + bounds.height + size.y +
				(height - (25 + bounds.height + size.y + bounds.height*scaleY)) / 2);
	transform.scale(scaleX, scaleY);

	// --- draw strings ---
	float[] point = new float[2];
	text = GraphicsExample.getResourceString("None"); //$NON-NLS-1$
	size = gc.stringExtent(text);
	point[0] = (scaleX*bounds.width + 5 - size.x)/(2*scaleX);
	point[1] = bounds.height;
	transform.transform(point);
	gc.drawString(text, (int)point[0], (int)point[1], true);

	text = GraphicsExample.getResourceString("Low"); //$NON-NLS-1$
	size = gc.stringExtent(text);
	point[0] = (scaleX*bounds.width + 5 - size.x)/(2*scaleX) + bounds.width;
	point[1] = bounds.height;
	transform.transform(point);
	gc.drawString(text, (int)point[0], (int)point[1], true);

	text = GraphicsExample.getResourceString("Default"); //$NON-NLS-1$
	size = gc.stringExtent(text);
	point[0] = (scaleX*bounds.width + 5 - size.x)/(2*scaleX) + 2*bounds.width;
	point[1] = bounds.height;
	transform.transform(point);
	gc.drawString(text, (int)point[0], (int)point[1], true);

	text = GraphicsExample.getResourceString("High"); //$NON-NLS-1$
	size = gc.stringExtent(text);
	point[0] = (scaleX*bounds.width + 5 - size.x)/(2*scaleX) + 3*bounds.width;
	point[1] = bounds.height;
	transform.transform(point);
	gc.drawString(text, (int)point[0], (int)point[1], true);

	gc.setTransform(transform);
	transform.dispose();

	// --- draw images ---

	// no interpolation
	gc.setInterpolation(SWT.NONE);
	gc.drawImage(image, 0, 0);

	// low interpolation
	gc.setInterpolation(SWT.LOW);
	gc.drawImage(image, bounds.width, 0);

	// default interpolation
	gc.setInterpolation(SWT.DEFAULT);
	gc.drawImage(image, 2*bounds.width, 0);

	// high interpolation
	gc.setInterpolation(SWT.HIGH);
	gc.drawImage(image, 3*bounds.width, 0);

	font.dispose();
	if (image != null) image.dispose();
}

/**
 * Returns the name of a valid font for the host platform.
 */
static String getPlatformFont() {
	if(SWT.getPlatform() == "win32") {
		return "Arial";
	} else if (SWT.getPlatform() == "gtk") {
		return "Baekmuk Batang";
	} else {
		return "Verdana";
	}
}
}

Back to the top