/** * @(#)MovingStars.java 1.0.1 98/12/04 *

* Copyright (C) 1998 David E. Wexler *

* This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. See * license.txt for a full copy of the GNU GPL. *

* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *

* You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. *

* To Contact the author send e-mail to vagabond@netdragon.com or send * snailmail to: 511 Bobcat Ct, Punta Gorda, FL 3398. */ import java.awt.*; import java.util.*; /** * MovingStars - Displays stars that move at random speeds. *

* This class generates a variable number of stars, puts them at * random locations, with random colors, and moves them at random * speeds. *

* @author David Wexler (vagabond@netdragons.com) * @version 1.0.1 * @since Nelzan1.0.1 */ class MovingStars { private int x[]; private int y[]; private int xMove[]; private int yMove[]; private Color color[]; private int height; private int width; private int maxXMove; private int maxYMove; private int length; /** * For generating random Numbers. */ Random r = new Random(); int i = 0; /** * Creates All the data to draw MovingStars. * * @param width The width of the screen. * @param height The height of the screen. * @param maxX Max speed the stars move in the X direction. * @param maxY Max speed the stars move in the Y direction. * @param count The number of stars to display. */ MovingStars(int width, int height, int maxX, int maxY, int count) { this.width = width; this.height = height; maxXMove = maxX; maxYMove = maxY; x = new int[count]; y = new int[count]; xMove = new int[count]; yMove = new int[count]; color = new Color[count]; length = count-1; genStart(); } /** * Updates the stars location. It will also generate a new star should the * current one be outside of visual range. */ public void update() { for (i = 0; i < length; i++) { x[i] -= xMove[i]; if (x[i] < 0) { x[i] = width; y[i] = abs(r.nextInt()%height); color[i] = new Color(abs(r.nextInt()%128)+128, abs(r.nextInt()%128)+128, abs(r.nextInt()%128)+128); } y[i] += yMove[i]; if (y[i] > height) { y[i] = -10; x[i] = abs(r.nextInt()%width); color[i] = new Color(abs(r.nextInt()%128)+128, abs(r.nextInt()%128)+128, abs(r.nextInt()%128)+128); } } } /** * Paints the stars. * * @param g Where to draw the stars. */ public void paint(Graphics g) { for (i = 0; i < length; i++) { g.setColor(color[i]); g.drawLine(x[i],y[i],x[i],y[i]); } } /** * Generates the stars starting location, everything is random. */ private void genStart() { for (i = 0; i < length; i++) { x[i] = abs(r.nextInt()%width); y[i] = abs(r.nextInt()%height); if (maxXMove == 0) xMove[i] = 0; else xMove[i] = abs(r.nextInt()%maxXMove) + 1; if (maxYMove == 0) yMove[i] = 0; else yMove[i] = abs(r.nextInt()%maxYMove) + 1; color[i] = new Color(abs(r.nextInt()%128)+128, abs(r.nextInt()%128)+128, abs(r.nextInt()%128)+128); } } /** * Returns the distance from zero. * * @param i The number to check. * @returns an int of the distance from zero. */ private int abs(int i) { if (i > 0) return i; return -i; } }