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
208
209
210
211
212
213
214
215
216
|
/*******************************************************************************
* Copyright (c) 2015, 2018 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.snippets;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* Snippet to test the use of the DPI-aware Image constructors.
* Work in progress in https://bugs.eclipse.org/399786
* <p>
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
public class Snippet367 {
private static final String IMAGE_100 = "eclipse16.png";
private static final String IMAGE_150 = "eclipse24.png";
private static final String IMAGE_200 = "eclipse32.png";
private static final String IMAGES_ROOT = "bin/org/eclipse/swt/snippets/";
private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100;
private static final String IMAGE_PATH_150 = IMAGES_ROOT + IMAGE_150;
private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200;
public static void main (String [] args) {
final ImageFileNameProvider filenameProvider = zoom -> {
switch (zoom) {
case 100:
return IMAGE_PATH_100;
case 150:
return IMAGE_PATH_150;
case 200:
return IMAGE_PATH_200;
default:
return null;
}
};
final ImageDataProvider imageDataProvider = zoom -> {
switch (zoom) {
case 100:
return new ImageData (IMAGE_PATH_100);
case 150:
return new ImageData (IMAGE_PATH_150);
case 200:
return new ImageData (IMAGE_PATH_200);
default:
return null;
}
};
final Display display = new Display ();
final Shell shell = new Shell (display);
shell.setText("Snippet367");
shell.setLayout (new GridLayout (3, false));
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
fileItem.setText("&File");
Menu fileMenu = new Menu(menuBar);
fileItem.setMenu(fileMenu);
MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
exitItem.setText("&Exit");
exitItem.addListener(SWT.Selection, e -> shell.close());
new Label (shell, SWT.NONE).setText (IMAGE_200 + ":");
new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_200));
new Button(shell, SWT.PUSH).setImage (new Image (display, IMAGE_PATH_200));
new Label (shell, SWT.NONE).setText (IMAGE_150 + ":");
new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_150));
new Button(shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_150));
new Label (shell, SWT.NONE).setText (IMAGE_100 + ":");
new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_100));
new Button(shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_100));
createSeparator(shell);
new Label (shell, SWT.NONE).setText ("ImageFileNameProvider:");
new Label (shell, SWT.NONE).setImage (new Image (display, filenameProvider));
new Button(shell, SWT.NONE).setImage (new Image (display, filenameProvider));
new Label (shell, SWT.NONE).setText ("ImageDataProvider:");
new Label (shell, SWT.NONE).setImage (new Image (display, imageDataProvider));
new Button(shell, SWT.NONE).setImage (new Image (display, imageDataProvider));
createSeparator(shell);
new Label (shell, SWT.NONE).setText ("1. Canvas\n(PaintListener)");
final Point size = new Point (550, 40);
final Canvas canvas = new Canvas (shell, SWT.NONE);
canvas.addPaintListener (e -> {
Point size1 = canvas.getSize ();
paintImage (e.gc, size1);
});
GridData gridData = new GridData (size.x, size.y);
gridData.horizontalSpan = 2;
canvas.setLayoutData (gridData);
createSeparator(shell);
new Label (shell, SWT.NONE).setText ("2. Painted image\n (default resolution)");
Image image = new Image (display, size.x, size.y);
GC gc = new GC (image);
try {
paintImage (gc, size);
} finally {
gc.dispose ();
}
Label imageLabel = new Label (shell, SWT.NONE);
imageLabel.setImage (image);
imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));
createSeparator(shell);
new Label (shell, SWT.NONE).setText ("3. Painted image\n(multi-res, unzoomed paint)");
imageLabel = new Label (shell, SWT.NONE);
imageLabel.setImage (new Image (display, (ImageDataProvider) zoom -> {
Image temp = new Image (display, size.x * zoom / 100, size.y * zoom / 100);
GC gc1 = new GC (temp);
try {
paintImage (gc1, size);
return temp.getImageData ();
} finally {
gc1.dispose ();
temp.dispose ();
}
}));
imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));
createSeparator(shell);
new Label (shell, SWT.NONE).setText ("4. Painted image\n(multi-res, zoomed paint)");
imageLabel = new Label (shell, SWT.NONE);
imageLabel.setImage (new Image (display, (ImageDataProvider) zoom -> {
Image temp = new Image (display, size.x * zoom / 100, size.y * zoom / 100);
GC gc1 = new GC (temp);
try {
paintImage2 (gc1, new Point (size.x * zoom / 100, size.y * zoom / 100), zoom / 100);
return temp.getImageData ();
} finally {
gc1.dispose ();
temp.dispose ();
}
}));
imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));
createSeparator(shell);
new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
Label box= new Label (shell, SWT.NONE);
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
box.setLayoutData (new GridData (50, 50));
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void createSeparator(Composite shell) {
new Label (shell, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, false, 3, 1));
}
private static void paintImage (GC gc, Point size) {
gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND));
gc.fillRectangle (0, 0, size.x, size.y);
gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_LIST_SELECTION));
gc.fillRoundRectangle (0, 0, size.x - 1, size.y - 1, 10, 10);
gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND));
gc.drawRoundRectangle (0, 0, size.x - 1, size.y - 1, 10, 10);
gc.drawText (gc.getFont ().getFontData ()[0].toString (), 10, 10, true);
}
private static void paintImage2 (GC gc, Point size, int f) {
gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND));
gc.fillRectangle (0, 0, size.x, size.y);
// Scale line width, corner roundness, and font size.
// Caveat: line width expands in all directions, so the origin also has to move.
gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_LIST_SELECTION));
gc.fillRoundRectangle (f/2, f/2, size.x - f, size.y - f, 10*f, 10*f);
gc.setBackground (gc.getDevice ().getSystemColor (SWT.COLOR_WIDGET_BACKGROUND));
gc.setLineWidth (f);
gc.drawRoundRectangle (f/2, f/2, size.x - f, size.y - f, 10*f, 10*f);
FontData fontData = gc.getFont ().getFontData ()[0];
fontData.setHeight (fontData.getHeight ()*f);
Font font = new Font (gc.getDevice (), fontData);
try {
gc.setFont (font);
gc.drawText (fontData.toString (), 10*f, 10*f, true);
} finally {
font.dispose ();
}
}
}
|