1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.swt;
12
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.Device;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.ImageData;
18 import org.eclipse.swt.graphics.PaletteData;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.graphics.Transform;
22
23
24
25
26
27
28
29 public class SwtGraphicsHelper {
30
31
32
33
34
35
36
37
38
39 public static void drawStringCentered(GC gc, String string, int left, int right, int y) {
40 Point extent = gc.textExtent(string);
41 int width = right - left;
42 int xx = (int) ((width - extent.x) / 2);
43 gc.drawString(string, left+xx, y);
44 }
45
46 public static void drawStringCenteredMidX(GC gc, String string, int midx, int y) {
47 Point extent = gc.textExtent(string);
48 int xx = (int) (midx - (extent.x / 2));
49 gc.drawString(string, xx, y);
50 }
51
52 public static void drawStringCenteredVCenter(GC gc, String string, int left, int right, int yCenter) {
53 Point extent = gc.textExtent(string);
54
55 int descent = 0;
56 int width = right - left;
57 int xx = (int) ((width - extent.x) / 2);
58 int y = yCenter - (int) (extent.y / 2 - descent);
59 gc.drawString(string, left + xx, y);
60
61 }
62
63 public static void drawStringRightAlignedVCenter(GC gc, String string, int x, int y) {
64 Point extent = gc.textExtent(string);
65
66 int xx = (int) (x - extent.x);
67 int yy = (int) (y - (extent.y / 2));
68 gc.drawString(string, xx, yy);
69 }
70
71 public static void drawStringLeftAlignedVCenter(GC gc, String string, int x, int y) {
72 Point extent = gc.textExtent(string);
73
74 int xx = x;
75 int yy = (int) (y - (extent.y / 2));
76 gc.drawString(string, xx, yy);
77 }
78
79 public static void drawStringCentered(GC gc, String string, int xCenter, int yBase) {
80 Point extent = gc.textExtent(string);
81 int xx = xCenter - (int) ((extent.x) / 2);
82 gc.drawText(string, xx, yBase - extent.y, true);
83 }
84
85 public static void drawStringCenteredAroundPoint(GC gc, String string, int xCenter, int yCenter) {
86 Point extent = gc.textExtent(string);
87 int xx = xCenter - (int) ((extent.x) / 2);
88 int yy = yCenter - (int) ((extent.y) / 2);
89 gc.drawText(string, xx, yy, true);
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public static void drawStringCentered(GC gc, String string, int x, int y, int width, int height) {
103 Point extent = gc.textExtent(string);
104 int xx = x + (width - extent.x) / 2;
105 int yy = y + (height - extent.y) / 2;
106 gc.drawText(string, xx, yy, true);
107 }
108
109
110
111
112
113
114
115
116 public static void drawStringCentered(GC gc, String string, Rectangle rect) {
117 drawStringCentered(gc, string, rect.x, rect.y, rect.width, rect.height);
118 }
119
120 public static int getStringDrawingWidth(GC gc, String string) {
121 return gc.textExtent(string).x;
122 }
123
124 public static int getStringDrawingHeight(GC gc, String string) {
125 return gc.textExtent(string).y;
126 }
127
128
129
130
131
132
133
134
135 public static void drawStringRightAlignedVTop(GC gc, String string, int x, int yTop) {
136 Point extent = gc.textExtent(string);
137 int xx = (int) (x - extent.x);
138 gc.drawText(string, xx, yTop, true);
139 }
140
141 public static void drawArrowLine(GC gc, int x1, int y1, int x2, int y2, int dist, int height, boolean arrowLeft,
142 boolean arrowRight) {
143 int off = height;
144 gc.drawLine(x1 + off + 1, y1, x2 - off - 1, y2);
145 if (arrowLeft) {
146 gc.drawLine(x1, y1, x1 + dist, y1 - off);
147 gc.drawLine(x1, y1, x1 + dist, y1 + off);
148 gc.drawLine(x1 + dist, y1 - off, x1 + dist, y1 + off);
149 }
150 if (arrowRight) {
151 gc.drawLine(x2, y2, x2 - dist, y2 - off);
152 gc.drawLine(x2, y2, x2 - dist, y2 + off);
153 gc.drawLine(x2 - dist, y2 - off, x2 - dist, y2 + off);
154 }
155 }
156
157 public static void drawArrowLineVertical(GC gc, int x1, int y1, int x2, int y2, int dist, int height,
158 boolean arrowUp, boolean arrowDown) {
159 int off = height;
160 gc.drawLine(x1, y1 + off + 1, x2, y2 - off - 1);
161 if (arrowUp) {
162 gc.drawLine(x1, y1, x1 - off, y1 + dist);
163 gc.drawLine(x1, y1, x1 + off, y1 + dist);
164 gc.drawLine(x1 - off, y1 + dist, x1 + off, y1 + dist);
165 }
166 if (arrowDown) {
167 gc.drawLine(x2, y2, x2 - off, y2 - dist);
168 gc.drawLine(x2, y2, x2 + off, y2 - dist);
169 gc.drawLine(x2 - off, y2 - dist, x2 + off, y2 - dist);
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182 public static void drawStringVCentered(GC gc, String label, int x, int upperY, int lowerY) {
183 Point extent = gc.textExtent(label);
184
185 int yy = (int) upperY + (lowerY - upperY - extent.y) / 2;
186 gc.drawText(label, x, yy, true);
187 }
188
189
190
191
192
193
194
195
196
197 public static void drawStringVertical(GC gc, String string, int x, int y) {
198 Point extent = gc.textExtent(string);
199 Image img = new Image(gc.getDevice(), extent.x, extent.y);
200 GC imageGC = new GC(img);
201 imageGC.drawString(string, 0, 0);
202 imageGC.dispose();
203
204
205 Image vertImg = new Image(gc.getDevice(), extent.y, extent.x);
206 ImageData iData = img.getImageData();
207 ImageData destIData = vertImg.getImageData();
208
209 for (int xx = 0; xx < iData.width; xx++) {
210 for (int yy = 0; yy < iData.height; yy++) {
211 destIData.setPixel(yy, iData.width-xx-1, iData.getPixel(xx, yy));
212 }
213 }
214
215 img.dispose();
216 Image destImg = new Image(gc.getDevice(), destIData);
217
218 gc.drawImage(destImg, x, y);
219 vertImg.dispose();
220 destImg.dispose();
221
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235 public static ImageData dropShadow(ImageData originalImageData, Color color, int radius, int highlightRadius,
236 int opacity) {
237
238
239
240
241
242 if (originalImageData == null)
243 return null;
244 if (color == null)
245 return null;
246 if (radius == 0)
247 return originalImageData;
248 int shift = (int) (radius * 1.5);
249
250 double highlightRadiusIncrease = radius < highlightRadius * 2 ? .15 : radius < highlightRadius * 3 ? .09 : .02;
251 opacity = opacity > 255 ? 255 : opacity < 0 ? 0 : opacity;
252
253 ImageData newImageData = new ImageData(originalImageData.width + radius * 2, originalImageData.height + radius
254 * 2, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000));
255 int[] pixels = new int[originalImageData.width];
256
257 for (int row = radius; row < radius + originalImageData.height; row++) {
258 originalImageData.getPixels(0, row - radius, originalImageData.width, pixels, 0);
259 for (int col = 0; col < pixels.length; col++)
260 pixels[col] = newImageData.palette.getPixel(originalImageData.palette.getRGB(pixels[col]));
261 newImageData.setPixels(radius, row, originalImageData.width, pixels, 0);
262 }
263
264 int colorInt = newImageData.palette.getPixel(color.getRGB());
265 pixels = new int[newImageData.width];
266 for (int i = 0; i < newImageData.width; i++) {
267 pixels[i] = colorInt;
268 }
269
270 byte[] alphas = new byte[newImageData.width];
271
272 for (int row = 0; row < newImageData.height; row++) {
273 if (row < radius) {
274
275 byte intensity = (byte) (opacity * ((((row + 1)) / (double) (radius))));
276 for (int col = 0; col < alphas.length / 2 + alphas.length % 2; col++) {
277 if (col < radius) {
278
279
280 double hypotenuse = Math
281 .sqrt(Math.pow(radius - col - 1, 2.0) + Math.pow(radius - 1 - row, 2.0));
282
283 alphas[col + shift] = alphas[alphas.length - col - 1] = (byte) (opacity * Math.max(
284 ((radius - hypotenuse) / radius), 0));
285
286 if (hypotenuse < Math.min(highlightRadius, radius * .5)) {
287 alphas[col + shift] = alphas[alphas.length - col - 1] = (byte) Math.min(255, (alphas[col
288 + shift] & 0x0FF)
289 * (1 + highlightRadiusIncrease * Math.max(((radius - hypotenuse) / radius), 0)));
290 }
291 } else {
292 alphas[col + shift] = alphas[alphas.length - col - 1] = (byte) ((row > Math.max(radius
293 - highlightRadius - 1, radius * .5)) ? Math.min(255, (intensity & 0x0FF)
294 * (1 + highlightRadiusIncrease * row / radius)) : intensity);
295 }
296 }
297 if (row + shift < newImageData.height) {
298 newImageData.setAlphas(newImageData.width - radius, row + shift, radius, alphas, alphas.length
299 - radius);
300 newImageData.setPixels(newImageData.width - radius, row + shift, radius, pixels, alphas.length
301 - radius);
302 }
303 newImageData.setAlphas(0, newImageData.height - 1 - row, newImageData.width, alphas, 0);
304 newImageData.setPixels(0, newImageData.height - 1 - row, newImageData.width, pixels, 0);
305 }
306
307 else if (row <= newImageData.height / 2) {
308
309 double intensity = 0;
310 for (int col = 0; col < alphas.length; col++) {
311 if (col < radius) {
312 intensity = (opacity * ((col + 1) / (double) radius));
313 if (col > Math.max(radius - highlightRadius - 1, radius * .5)) {
314 intensity = Math.min(255, (intensity) * (1 + highlightRadiusIncrease * col / radius));
315 }
316 alphas[newImageData.width - col - 1] = (byte) (int) (intensity);
317 alphas[col] = 0;
318 } else if (col <= newImageData.width / 2 + newImageData.width % 2) {
319
320 alphas[col] = alphas[newImageData.width - col - 1] = (byte) (255);
321 }
322 }
323 newImageData.setPixels(0, newImageData.height - 1 - row, radius, pixels, 0);
324 newImageData.setPixels(originalImageData.width + radius, newImageData.height - 1 - row, radius, pixels,
325 0);
326 newImageData.setAlphas(0, newImageData.height - 1 - row, newImageData.width, alphas, 0);
327 if (row >= shift + radius) {
328 newImageData.setPixels(0, row, radius, pixels, 0);
329 newImageData.setPixels(originalImageData.width + radius, row, radius, pixels, 0);
330 newImageData.setAlphas(0, row, newImageData.width, alphas, 0);
331 } else {
332 newImageData.setPixels(0, row, radius, pixels, 0);
333 newImageData.setAlphas(0, row, newImageData.width - radius, alphas, 0);
334 }
335 }
336 }
337 return newImageData;
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public static ImageData glow(ImageData originalImageData, Color color, int radius, int highlightRadius, int opacity) {
353
354
355
356
357
358
359
360
361
362
363
364
365 if (originalImageData == null)
366 return null;
367 if (color == null)
368 return null;
369 if (radius == 0)
370 return originalImageData;
371
372 double highlightRadiusIncrease = radius < highlightRadius * 2 ? .15 : radius < highlightRadius * 3 ? .09 : .02;
373 opacity = opacity > 255 ? 255 : opacity < 0 ? 0 : opacity;
374
375 ImageData newImageData = new ImageData(originalImageData.width + radius * 2, originalImageData.height + radius
376 * 2, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000));
377 int[] pixels = new int[originalImageData.width];
378
379 for (int row = radius; row < radius + originalImageData.height; row++) {
380 originalImageData.getPixels(0, row - radius, originalImageData.width, pixels, 0);
381 for (int col = 0; col < pixels.length; col++)
382 pixels[col] = newImageData.palette.getPixel(originalImageData.palette.getRGB(pixels[col]));
383 newImageData.setPixels(radius, row, originalImageData.width, pixels, 0);
384 }
385
386 int colorInt = newImageData.palette.getPixel(color.getRGB());
387 pixels = new int[newImageData.width];
388 for (int i = 0; i < newImageData.width; i++) {
389 pixels[i] = colorInt;
390 }
391
392 byte[] alphas = new byte[newImageData.width];
393
394 for (int row = 0; row < newImageData.height; row++) {
395 if (row < radius) {
396
397 byte intensity = (byte) (opacity * ((((row + 1)) / (double) (radius))));
398 for (int col = 0; col < alphas.length / 2 + alphas.length % 2; col++) {
399 if (col < radius) {
400
401
402 double hypotenuse = Math
403 .sqrt(Math.pow(radius - col - 1, 2.0) + Math.pow(radius - 1 - row, 2.0));
404
405 alphas[col] = alphas[alphas.length - col - 1] = (byte) (opacity * Math.max(
406 ((radius - hypotenuse) / radius), 0));
407
408 if (hypotenuse < Math.min(highlightRadius, radius * .5)) {
409 alphas[col] = alphas[alphas.length - col - 1] = (byte) Math.min(255, (alphas[col] & 0x0FF)
410 * (1 + highlightRadiusIncrease * Math.max(((radius - hypotenuse) / radius), 0)));
411 }
412 } else {
413 alphas[col] = alphas[alphas.length - 1 - col] = (byte) ((row > Math.max(radius
414 - highlightRadius - 1, radius * .5)) ? Math.min(255, (intensity & 0x0FF)
415 * (1 + highlightRadiusIncrease * row / radius)) : intensity);
416 }
417 }
418 newImageData.setAlphas(0, row, newImageData.width, alphas, 0);
419 newImageData.setAlphas(0, newImageData.height - 1 - row, newImageData.width, alphas, 0);
420 newImageData.setPixels(0, row, newImageData.width, pixels, 0);
421 newImageData.setPixels(0, newImageData.height - 1 - row, newImageData.width, pixels, 0);
422 }
423
424 else if (row <= newImageData.height / 2) {
425
426 double intensity = 0;
427 for (int col = 0; col < alphas.length; col++) {
428 if (col < radius) {
429 intensity = (opacity * ((col + 1) / (double) radius));
430 if (col > Math.max(radius - highlightRadius - 1, radius * .5)) {
431 intensity = Math.min(255, (intensity) * (1 + highlightRadiusIncrease * col / radius));
432 }
433 alphas[col] = alphas[newImageData.width - col - 1] = (byte) (intensity);
434 } else if (col <= newImageData.width / 2 + newImageData.width % 2) {
435
436 alphas[col] = alphas[newImageData.width - col - 1] = (byte) (255);
437 }
438 }
439 newImageData.setPixels(0, row, radius, pixels, 0);
440 newImageData.setPixels(originalImageData.width + radius, row, radius, pixels, 0);
441 newImageData.setAlphas(0, row, newImageData.width, alphas, 0);
442 newImageData.setPixels(0, newImageData.height - 1 - row, radius, pixels, 0);
443 newImageData.setPixels(originalImageData.width + radius, newImageData.height - 1 - row, radius, pixels,
444 0);
445 newImageData.setAlphas(0, newImageData.height - 1 - row, newImageData.width, alphas, 0);
446 }
447 }
448 return newImageData;
449 }
450
451
452
453
454
455
456
457
458 public static Image reflect(Image img, Device device) {
459
460 int height = img.getImageData().height;
461 int width = img.getImageData().width;
462
463 Image reflect = new Image(device, width, height / 2);
464 GC imageGC = new GC(reflect);
465
466 Transform rTransform = new Transform(imageGC.getDevice(), 1, 0, 0, -.5f, 0, height / 2);
467 imageGC.setTransform(rTransform);
468
469 imageGC.setAlpha(100);
470
471 imageGC.drawImage(img, 0, 0);
472
473
474
475
476
477
478
479
480
481
482
483
484 imageGC.dispose();
485 return reflect;
486 }
487
488 }