Skip to content

Commit f1bf0d3

Browse files
vogellaclaude
andcommitted
Migrate 6 OldSchoolEffect examples to GraphicsExample
Migrates 6 image-based effects from SWT-OldSchoolEffect: - Block Effect (rayfilter/BlockEffect.java + flower.jpg) - Twirl Effect (rayfilter/TwirlEffect.java + flower.jpg) - Sine Wave (sinewave/SineWave.java + tuxblackbg.png) - Sky (sky2/Sky2.java + DEMO2.png) - Unlimited Balls (unlimitedballs/UnlimitedBalls.java + ball.gif) - Warp (warp/Warp.java + texture.png) Contributes to #3189 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 57fe5a5 commit f1bf0d3

File tree

12 files changed

+1182
-0
lines changed

12 files changed

+1182
-0
lines changed

examples/org.eclipse.swt.examples/src/examples_graphics.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,21 @@ FlatTextDescription=This is a miscellaneous demonstration of an animated flat te
238238

239239
Lens=Lens
240240
LensDescription=This is a miscellaneous demonstration of an animated spherical lens distortion effect that bounces across an image, magnifying the area beneath the lens.
241+
242+
BlockEffect=Block Effect
243+
BlockEffectDescription=This is a miscellaneous demonstration of an animated block pixelation effect that oscillates block size from single pixels to large blocks and back on an image.
244+
245+
TwirlEffect=Twirl Effect
246+
TwirlEffectDescription=This is a miscellaneous demonstration of an animated twirl distortion effect that rotates pixels around the center of an image with increasing and decreasing twirl angle.
247+
248+
SineWave=Sine Wave
249+
SineWaveDescription=This is a miscellaneous demonstration of an animated sine wave distortion effect that cycles through 12 different wave configurations applied to an image.
250+
251+
Sky=Sky
252+
SkyDescription=This is a miscellaneous demonstration of an animated sky/landscape effect using perspective-corrected texture mapping with scrolling sprites and shadows.
253+
254+
UnlimitedBalls=Unlimited Balls
255+
UnlimitedBallsDescription=This is a miscellaneous demonstration of an animated unlimited balls effect where ball sprites are drawn along Lissajous-like curves using multiple offscreen buffers.
256+
257+
Warp=Warp
258+
WarpDescription=This is a miscellaneous demonstration of an animated warp distortion effect that uses a precomputed distortion lookup table to render a texture with animated warping.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Laurent Caron and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Jerry Huxtable - Original Version
13+
* Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT
14+
* IBM Corporation - adaptation to GraphicsExample
15+
*******************************************************************************/
16+
17+
package org.eclipse.swt.examples.graphics;
18+
19+
import org.eclipse.swt.graphics.GC;
20+
import org.eclipse.swt.graphics.Image;
21+
import org.eclipse.swt.graphics.ImageData;
22+
23+
/**
24+
* This tab displays an animated block pixelation effect that oscillates block
25+
* size from single pixels to large blocks and back on an image.
26+
*/
27+
public class BlockEffectTab extends AnimatedGraphicsTab {
28+
29+
private ImageData sourceImage;
30+
private ImageData imageData;
31+
private Image outputImage;
32+
private int imgWidth, imgHeight;
33+
private int blockSize;
34+
private int direction;
35+
36+
public BlockEffectTab(GraphicsExample example) {
37+
super(example);
38+
}
39+
40+
@Override
41+
public String getCategory() {
42+
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
43+
}
44+
45+
@Override
46+
public String getText() {
47+
return GraphicsExample.getResourceString("BlockEffect"); //$NON-NLS-1$
48+
}
49+
50+
@Override
51+
public String getDescription() {
52+
return GraphicsExample.getResourceString("BlockEffectDescription"); //$NON-NLS-1$
53+
}
54+
55+
@Override
56+
public int getInitialAnimationTime() {
57+
return 10;
58+
}
59+
60+
@Override
61+
public void dispose() {
62+
if (outputImage != null) {
63+
outputImage.dispose();
64+
outputImage = null;
65+
}
66+
}
67+
68+
@Override
69+
public void next(int width, int height) {
70+
if (sourceImage == null) {
71+
return;
72+
}
73+
74+
imageData = filter(sourceImage, blockSize);
75+
blockSize += direction;
76+
77+
if (blockSize <= 1) {
78+
direction = 1;
79+
blockSize = 1;
80+
}
81+
82+
if (blockSize >= imgWidth / 4) {
83+
direction = -1;
84+
}
85+
}
86+
87+
@Override
88+
public void paint(GC gc, int width, int height) {
89+
if (!example.checkAdvancedGraphics()) return;
90+
91+
if (sourceImage == null) {
92+
Image loaded = example.loadImage(gc.getDevice(), "flower.jpg"); //$NON-NLS-1$
93+
if (loaded == null) return;
94+
sourceImage = loaded.getImageData();
95+
imgWidth = sourceImage.width;
96+
imgHeight = sourceImage.height;
97+
98+
int[] pixels = new int[imgWidth * imgHeight];
99+
sourceImage.getPixels(0, 0, imgWidth * imgHeight, pixels, 0);
100+
imageData = new ImageData(imgWidth, imgHeight, sourceImage.depth, sourceImage.palette);
101+
imageData.setPixels(0, 0, imgWidth * imgHeight, pixels, 0);
102+
103+
blockSize = 1;
104+
direction = 1;
105+
}
106+
107+
if (imageData == null) {
108+
return;
109+
}
110+
111+
if (outputImage != null) {
112+
outputImage.dispose();
113+
}
114+
outputImage = new Image(gc.getDevice(), imageData);
115+
116+
int x = (width - imgWidth) / 2;
117+
int y = (height - imgHeight) / 2;
118+
gc.drawImage(outputImage, x, y);
119+
}
120+
121+
private ImageData filter(ImageData src, int size) {
122+
if (size <= 1) {
123+
int[] pixels = new int[imgWidth * imgHeight];
124+
src.getPixels(0, 0, imgWidth * imgHeight, pixels, 0);
125+
ImageData copy = new ImageData(imgWidth, imgHeight, src.depth, src.palette);
126+
copy.setPixels(0, 0, imgWidth * imgHeight, pixels, 0);
127+
return copy;
128+
}
129+
130+
ImageData dst = new ImageData(imgWidth, imgHeight, src.depth, src.palette);
131+
int[] pixels = new int[size * size];
132+
133+
for (int y = 0; y < imgHeight; y += size) {
134+
for (int x = 0; x < imgWidth; x += size) {
135+
int w = Math.min(size, imgWidth - x);
136+
int h = Math.min(size, imgHeight - y);
137+
int t = w * h;
138+
getRGB(src, x, y, w, h, pixels, 0, w);
139+
int r = 0, g = 0, b = 0;
140+
int i = 0;
141+
for (int by = 0; by < h; by++) {
142+
for (int bx = 0; bx < w; bx++) {
143+
int argb = pixels[i];
144+
r += (argb >> 16) & 0xff;
145+
g += (argb >> 8) & 0xff;
146+
b += argb & 0xff;
147+
i++;
148+
}
149+
}
150+
int argb = ((r / t) << 16) | ((g / t) << 8) | (b / t);
151+
i = 0;
152+
for (int by = 0; by < h; by++) {
153+
for (int bx = 0; bx < w; bx++) {
154+
pixels[i] = (pixels[i] & 0xff000000) | argb;
155+
i++;
156+
}
157+
}
158+
setRGB(dst, x, y, w, h, pixels, 0, w);
159+
}
160+
}
161+
return dst;
162+
}
163+
164+
private static void getRGB(ImageData image, int startX, int startY, int w, int h, int[] pixels, int offset, int scansize) {
165+
int yoff = offset;
166+
for (int y = startY; y < startY + h; y++, yoff += scansize) {
167+
int off = yoff;
168+
for (int x = startX; x < startX + w; x++) {
169+
pixels[off++] = image.getPixel(x, y);
170+
}
171+
}
172+
}
173+
174+
private static void setRGB(ImageData image, int startX, int startY, int w, int h, int[] pixels, int offset, int scansize) {
175+
int yoff = offset;
176+
for (int y = startY; y < startY + h; y++, yoff += scansize) {
177+
int off = yoff;
178+
for (int x = startX; x < startX + w; x++) {
179+
image.setPixel(x, y, pixels[off++]);
180+
}
181+
}
182+
}
183+
}
49.3 KB
Loading

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/graphics/GraphicsExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ GraphicsTab[] createTabs() {
495495
new BumpMappingTab(this),
496496
new FlatTextTab(this),
497497
new LensTab(this),
498+
new BlockEffectTab(this),
499+
new TwirlEffectTab(this),
500+
new SineWaveTab(this),
501+
new SkyTab(this),
502+
new UnlimitedBallsTab(this),
503+
new WarpTab(this),
498504
};
499505
}
500506

0 commit comments

Comments
 (0)