Skip to content

Commit 84a9535

Browse files
vogellaakurtakov
authored andcommitted
Migrate Starfield example to Graphics Example
This change migrates the Starfield animation effect from the SWT-OldSchoolEffect repository to the standard SWT Graphics Example. - Created StarfieldTab.java by extending AnimatedGraphicsTab. - Registered StarfieldTab in GraphicsExample.java. - Added localized strings to examples_graphics.properties.
1 parent eb32bd0 commit 84a9535

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Rectangle=Rectangle
8383
Oval=Oval
8484
Word=Word
8585
Star=Star
86+
Starfield=Starfield
8687
Triangles=Triangles
8788
Default=Default
8889
RegionClipping=Region Clipping
@@ -184,5 +185,6 @@ rgbDescription=Miscellaneous tab that demonstrates emerging colors from layering
184185
RegionClippingDescription=This tab demonstrates how to apply a region clipping and the effects of applying one. It also demonstrates the operations that can be applied between two regions.
185186
ShapesDescription=This tab draws 3D shapes (in 2D) using various line styles.
186187
SpiralDescription=Miscellaneous tab that presents a spiral consisting of the number of petals specified.
188+
StarfieldDescription=This is a miscellaneous demonstration that uses the drawPoint() operation to simulate a moving starfield.
187189
StarPolygonDescription=This tab draws a polygon and shows the effects of setting different fill rules: SWT.FILL_WINDING or SWT.FILL_EVEN_ODD
188190
AntialiasingTextDesc=This tab demonstrates antialiasing for text. Antialiasing is used for smoothing jagged edges in graphics. This tab allows the user to see the effects of different antialiasing values.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ GraphicsTab[] createTabs() {
469469
new LineStyleTab(this),
470470
new LineJoinTab(this),
471471
new RegionClippingTab(this),
472+
new StarfieldTab(this),
472473
new CustomAlphaTab(this),
473474
new TextAntialiasTab(this),
474475
new GraphicAntialiasTab(this),
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
* Laurent Caron (laurent.caron at gmail dot com) - Original Version
13+
* IBM Corporation - adaptation to GraphicsExample
14+
*******************************************************************************/
15+
16+
package org.eclipse.swt.examples.graphics;
17+
18+
import org.eclipse.swt.SWT;
19+
import org.eclipse.swt.graphics.Color;
20+
import org.eclipse.swt.graphics.GC;
21+
22+
/**
23+
* This tab displays a starfield animation.
24+
*/
25+
public class StarfieldTab extends AnimatedGraphicsTab {
26+
27+
private static final int NUMBER_OF_STARS = 1020;
28+
private Star[] stars;
29+
30+
static class Star {
31+
float xpos, ypos;
32+
int zpos, speed;
33+
int color;
34+
}
35+
36+
public StarfieldTab(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("Starfield"); //$NON-NLS-1$
48+
}
49+
50+
@Override
51+
public String getDescription() {
52+
return GraphicsExample.getResourceString("StarfieldDescription"); //$NON-NLS-1$
53+
}
54+
55+
@Override
56+
public int getInitialAnimationTime() {
57+
return 10;
58+
}
59+
60+
@Override
61+
public void next(int width, int height) {
62+
if (stars == null) return;
63+
64+
for (int i = 0; i < NUMBER_OF_STARS; i++) {
65+
stars[i].zpos -= stars[i].speed;
66+
67+
if (stars[i].zpos <= 0) {
68+
stars[i] = initStar(i + 1);
69+
}
70+
71+
/* compute 3D position */
72+
final int tempx = (int) (stars[i].xpos / stars[i].zpos + width / 2);
73+
final int tempy = (int) (stars[i].ypos / stars[i].zpos + height / 2);
74+
75+
if (tempx < 0 || tempx > width - 1 || tempy < 0 || tempy > height - 1) /* check if a star leaves the screen */
76+
{
77+
stars[i] = initStar(i + 1);
78+
}
79+
}
80+
}
81+
82+
@Override
83+
public void paint(GC gc, int width, int height) {
84+
if (stars == null) {
85+
stars = new Star[NUMBER_OF_STARS];
86+
for (int i = 0; i < NUMBER_OF_STARS; i++) {
87+
stars[i] = initStar(i + 1);
88+
}
89+
}
90+
91+
gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));
92+
gc.fillRectangle(0, 0, width, height);
93+
94+
for (int i = 0; i < NUMBER_OF_STARS; i++) {
95+
/* compute 3D position */
96+
final int tempx = (int) (stars[i].xpos / stars[i].zpos + width / 2);
97+
final int tempy = (int) (stars[i].ypos / stars[i].zpos + height / 2);
98+
99+
if (tempx >= 0 && tempx < width && tempy >= 0 && tempy < height) {
100+
Color color = new Color(gc.getDevice(), stars[i].color, stars[i].color, stars[i].color);
101+
gc.setForeground(color);
102+
gc.drawPoint(tempx, tempy);
103+
color.dispose();
104+
}
105+
}
106+
}
107+
108+
private Star initStar(int i) {
109+
final Star star = new Star();
110+
star.xpos = (float) (-10.0 + 20.0 * Math.random());
111+
star.ypos = (float) (-10.0 + 20.0 * Math.random());
112+
113+
star.xpos *= 3072.0; /* change viewpoint */
114+
star.ypos *= 3072.0;
115+
116+
star.zpos = i;
117+
star.speed = 2 + (int) (2.0 * Math.random());
118+
119+
star.color = i >> 2; /* the closer to the viewer the brighter */
120+
121+
return star;
122+
}
123+
}

0 commit comments

Comments
 (0)