Skip to content

Commit 5cf4777

Browse files
vogellaCopilot
andcommitted
Migrate 5 effects to Graphics Example: Explosion, Mandelbrot, ShadeBobs, Twister, Wave
Migrate the following effects from SWT-OldSchoolEffect to GraphicsExample: - ExplosionTab: Particle system with fire propagation - MandelbrotTab: Animated Mandelbrot fractal with zoom - ShadeBobsTab: Heat trail shade bobs effect - TwisterTab: Torsion deformation on checkered pattern - WaveTab: Multiple wave motion patterns Each effect extends AnimatedGraphicsTab, is registered in GraphicsExample.createTabs(), and has localized strings in examples_graphics.properties. Fixes #3189 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9e86574 commit 5cf4777

File tree

7 files changed

+950
-0
lines changed

7 files changed

+950
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,18 @@ BlobDescription=This is a miscellaneous demonstration of an animated metaball ef
196196

197197
Plasma=Plasma
198198
PlasmaDescription=This is a miscellaneous demonstration of an animated plasma effect using precomputed sine tables and a cycling color palette.
199+
200+
Explosion=Explosion
201+
ExplosionDescription=This is a miscellaneous demonstration of an animated explosion effect using a particle system and fire propagation algorithm.
202+
203+
Mandelbrot=Mandelbrot
204+
MandelbrotDescription=This is a miscellaneous demonstration of an animated Mandelbrot fractal with a continuously zooming view into the fractal boundary.
205+
206+
ShadeBobs=Shade Bobs
207+
ShadeBobsDescription=This is a miscellaneous demonstration of an animated shade bobs effect where a heated bob shape moves along a precomputed path, leaving a fading heat trail.
208+
209+
Twister=Twister
210+
TwisterDescription=This is a miscellaneous demonstration of an animated twister effect using precomputed sine tables and a torsion deformation on a checkered pattern.
211+
212+
Wave=Wave
213+
WaveDescription=This is a miscellaneous demonstration of an animated wave effect with multiple motion patterns cycling through four different wave algorithms.
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
* W.P. van Paassen - 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.SWT;
20+
import org.eclipse.swt.graphics.GC;
21+
import org.eclipse.swt.graphics.Image;
22+
import org.eclipse.swt.graphics.ImageData;
23+
import org.eclipse.swt.graphics.PaletteData;
24+
import org.eclipse.swt.graphics.RGB;
25+
26+
/**
27+
* This tab displays an animated explosion effect using a particle system
28+
* and fire propagation algorithm.
29+
*/
30+
public class ExplosionTab extends AnimatedGraphicsTab {
31+
32+
private static final int NUMBER_OF_PARTICLES = 500;
33+
34+
private ImageData imageData;
35+
private Image outputImage;
36+
private int[] fire;
37+
private Particle[] particles;
38+
private RGB[] colors;
39+
private int lastWidth;
40+
private int lastHeight;
41+
42+
private static class Particle {
43+
int xpos, ypos, xdir, ydir;
44+
int colorindex;
45+
boolean dead;
46+
}
47+
48+
public ExplosionTab(GraphicsExample example) {
49+
super(example);
50+
}
51+
52+
@Override
53+
public String getCategory() {
54+
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
55+
}
56+
57+
@Override
58+
public String getText() {
59+
return GraphicsExample.getResourceString("Explosion"); //$NON-NLS-1$
60+
}
61+
62+
@Override
63+
public String getDescription() {
64+
return GraphicsExample.getResourceString("ExplosionDescription"); //$NON-NLS-1$
65+
}
66+
67+
@Override
68+
public int getInitialAnimationTime() {
69+
return 10;
70+
}
71+
72+
@Override
73+
public void dispose() {
74+
if (outputImage != null) {
75+
outputImage.dispose();
76+
outputImage = null;
77+
}
78+
}
79+
80+
@Override
81+
public void next(int width, int height) {
82+
if (fire == null) {
83+
return;
84+
}
85+
if (width != lastWidth || height != lastHeight) {
86+
init(width, height);
87+
}
88+
89+
// Move and draw particles into fire array
90+
int nbDead = 0;
91+
for (int i = 0; i < NUMBER_OF_PARTICLES; i++) {
92+
if (!particles[i].dead) {
93+
particles[i].xpos += particles[i].xdir;
94+
particles[i].ypos += particles[i].ydir;
95+
96+
if (particles[i].ypos >= height - 3 || particles[i].colorindex == 0
97+
|| particles[i].xpos <= 1 || particles[i].xpos >= width - 3) {
98+
particles[i].dead = true;
99+
continue;
100+
}
101+
102+
particles[i].ydir++;
103+
particles[i].colorindex--;
104+
105+
final int temp = particles[i].ypos * width + particles[i].xpos;
106+
fire[temp] = particles[i].colorindex;
107+
fire[temp - 1] = particles[i].colorindex;
108+
fire[temp + width] = particles[i].colorindex;
109+
fire[temp - width] = particles[i].colorindex;
110+
fire[temp + 1] = particles[i].colorindex;
111+
} else {
112+
nbDead++;
113+
}
114+
}
115+
116+
// Create fire effect
117+
for (int i = 1; i < height - 2; i++) {
118+
final int index = (i - 1) * width;
119+
for (int j = 1; j < width - 2; j++) {
120+
int buf = index + j;
121+
int temp = fire[buf];
122+
temp += fire[buf + 1];
123+
temp += fire[buf - 1];
124+
buf += width;
125+
temp += fire[buf - 1];
126+
temp += fire[buf + 1];
127+
buf += width;
128+
temp += fire[buf];
129+
temp += fire[buf + 1];
130+
temp += fire[buf - 1];
131+
temp >>= 3;
132+
if (temp > 4) {
133+
temp -= 4;
134+
} else {
135+
temp = 0;
136+
}
137+
fire[buf - width] = temp;
138+
}
139+
}
140+
141+
// Draw fire array to image from bottom to top
142+
int pixel = width * height - 1;
143+
for (int i = height - 1; i >= 0; --i) {
144+
final int temp = i * width;
145+
for (int j = width - 1; j >= 0; --j) {
146+
imageData.setPixel(pixel % width, pixel / width, fire[temp + j]);
147+
pixel--;
148+
}
149+
}
150+
151+
if (nbDead == NUMBER_OF_PARTICLES) {
152+
initParticles(width, height);
153+
}
154+
}
155+
156+
@Override
157+
public void paint(GC gc, int width, int height) {
158+
if (fire == null) {
159+
init(width, height);
160+
}
161+
162+
gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));
163+
gc.fillRectangle(0, 0, width, height);
164+
165+
if (imageData == null) {
166+
return;
167+
}
168+
169+
if (outputImage != null) {
170+
outputImage.dispose();
171+
}
172+
outputImage = new Image(gc.getDevice(), imageData);
173+
gc.drawImage(outputImage, 0, 0);
174+
}
175+
176+
private void init(int width, int height) {
177+
lastWidth = width;
178+
lastHeight = height;
179+
180+
fire = new int[width * height];
181+
particles = new Particle[NUMBER_OF_PARTICLES];
182+
183+
colors = new RGB[256];
184+
for (int i = 0; i < 32; ++i) {
185+
colors[i] = new RGB(0, 0, i << 1);
186+
colors[i + 32] = new RGB(i << 3, 0, 64 - (i << 1));
187+
colors[i + 64] = new RGB(255, i << 3, 0);
188+
colors[i + 96] = new RGB(255, 255, i << 2);
189+
colors[i + 128] = new RGB(255, 255, 64 + (i << 2));
190+
colors[i + 160] = new RGB(255, 255, 128 + (i << 2));
191+
colors[i + 192] = new RGB(255, 255, 192 + i);
192+
colors[i + 224] = new RGB(255, 255, 224 + i);
193+
}
194+
195+
imageData = new ImageData(width, height, 8, new PaletteData(colors));
196+
197+
for (int i = 0; i < NUMBER_OF_PARTICLES; i++) {
198+
particles[i] = new Particle();
199+
}
200+
initParticles(width, height);
201+
}
202+
203+
private void initParticles(int width, int height) {
204+
for (int i = 0; i < NUMBER_OF_PARTICLES; i++) {
205+
particles[i].xpos = (width >> 1) - 20 + (int) (40.0 * Math.random());
206+
particles[i].ypos = (height >> 1) - 20 + (int) (40.0 * Math.random());
207+
particles[i].xdir = -10 + (int) (20.0 * Math.random());
208+
particles[i].ydir = -17 + (int) (19.0 * Math.random());
209+
particles[i].colorindex = 255;
210+
particles[i].dead = false;
211+
}
212+
}
213+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ GraphicsTab[] createTabs() {
481481
new TransformReuseTab(this),
482482
new PathReuseTab(this),
483483
new PlasmaTab(this),
484+
new ExplosionTab(this),
485+
new MandelbrotTab(this),
486+
new ShadeBobsTab(this),
487+
new TwisterTab(this),
488+
new WaveTab(this),
484489
};
485490
}
486491

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 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+
* Calogiuri Enzo Antonio (Insolit Dust) - 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.SWT;
20+
import org.eclipse.swt.graphics.GC;
21+
import org.eclipse.swt.graphics.Image;
22+
import org.eclipse.swt.graphics.ImageData;
23+
import org.eclipse.swt.graphics.PaletteData;
24+
25+
/**
26+
* This tab displays an animated Mandelbrot fractal with a continuously
27+
* zooming view into the fractal boundary.
28+
*/
29+
public class MandelbrotTab extends AnimatedGraphicsTab {
30+
31+
private static final int MAX_ITER = 570;
32+
33+
private ImageData imageData;
34+
private Image outputImage;
35+
private double zoom = 150;
36+
private double moveX = -0.5;
37+
private double moveY = 0.0;
38+
private double zoomSpeed = 1.02;
39+
private int lastWidth;
40+
private int lastHeight;
41+
42+
public MandelbrotTab(GraphicsExample example) {
43+
super(example);
44+
}
45+
46+
@Override
47+
public String getCategory() {
48+
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
49+
}
50+
51+
@Override
52+
public String getText() {
53+
return GraphicsExample.getResourceString("Mandelbrot"); //$NON-NLS-1$
54+
}
55+
56+
@Override
57+
public String getDescription() {
58+
return GraphicsExample.getResourceString("MandelbrotDescription"); //$NON-NLS-1$
59+
}
60+
61+
@Override
62+
public int getInitialAnimationTime() {
63+
return 50;
64+
}
65+
66+
@Override
67+
public void dispose() {
68+
if (outputImage != null) {
69+
outputImage.dispose();
70+
outputImage = null;
71+
}
72+
}
73+
74+
@Override
75+
public void next(int width, int height) {
76+
if (imageData == null) {
77+
return;
78+
}
79+
if (width != lastWidth || height != lastHeight) {
80+
init(width, height);
81+
}
82+
83+
zoom *= zoomSpeed;
84+
drawMandelbrot(width, height);
85+
}
86+
87+
@Override
88+
public void paint(GC gc, int width, int height) {
89+
if (imageData == null) {
90+
init(width, height);
91+
}
92+
93+
gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));
94+
gc.fillRectangle(0, 0, width, height);
95+
96+
if (imageData == null) {
97+
return;
98+
}
99+
100+
if (outputImage != null) {
101+
outputImage.dispose();
102+
}
103+
outputImage = new Image(gc.getDevice(), imageData);
104+
gc.drawImage(outputImage, 0, 0);
105+
}
106+
107+
private void init(int width, int height) {
108+
lastWidth = width;
109+
lastHeight = height;
110+
zoom = 150;
111+
imageData = new ImageData(width, height, 24, new PaletteData(0xFF0000, 0xFF00, 0xFF));
112+
}
113+
114+
private void drawMandelbrot(int width, int height) {
115+
for (int y = 0; y < height; y++) {
116+
for (int x = 0; x < width; x++) {
117+
double zx = 0;
118+
double zy = 0;
119+
final double cX = (x - width / 2.0) / zoom + moveX;
120+
final double cY = (y - height / 2.0) / zoom + moveY;
121+
double tmp;
122+
int iter = MAX_ITER;
123+
while (zx * zx + zy * zy < 4 && iter > 0) {
124+
tmp = zx * zx - zy * zy + cX;
125+
zy = 2.0 * zx * zy + cY;
126+
zx = tmp;
127+
iter--;
128+
}
129+
imageData.setPixel(x, y, iter | iter << 8);
130+
}
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)