|
| 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