Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Seeing Stars
|
Posted: Mar 3, 2002 11:07 AM
|
|
Well, now that we've raised the bar, lets make a couple more improvements: stars that don't include the internal crossing lines and which are vertically and horizontally symmetrical. As before, this can be run as an applet or an application (with a new command-line option of "/spiky" to draw the old simple-style spiky stars). This also includes all the features in the previous posts (random colors, positioning, sizing, number of points from 4 to 6, opionally specified star count, etc.).
// NightSky.java
import java.awt.*;
import java.applet.Applet;
import java.util.Random;
public class NightSky extends Applet
{
public static void main(String[] args)
{
Applet applet = null;
boolean traditional = true;
int starCount = 0;
for( int i = 0; i < args.length; i++ )
{
if( args[i].equals("/spiky") )
traditional = false;
try
{
starCount = Integer.parseInt(args[i]);
}
catch( NumberFormatException nfe )
{
// Usually a bad idea to "eat" an exception, but in this case,
// it is not a severe problem, since there will be a reasonable
// default value for the star count.
}
}
if( starCount > 0 )
applet = new NightSky( starCount, traditional );
else
applet = new NightSky(traditional);
javax.swing.JFrame frame = new javax.swing.JFrame("Night Sky");
// To close the application:
frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add(applet);
frame.setSize(350,350);
applet.init();
applet.start();
frame.setVisible(true);
}
private boolean traditional = true;
private int starCount = 50;
private Star [] stars;
private static final Random random = new Random();
private static Color[] colors = { Color.red, Color.yellow, Color.green,
Color.cyan, Color.magenta, Color.blue };
public NightSky()
{
}
public NightSky(boolean traditionalStars)
{
traditional = traditionalStars;
}
public NightSky( int starCount, boolean traditionalStars )
{
traditional = traditionalStars;
this.starCount = starCount;
}
public void init ()
{
stars = new Star[starCount];
for( int i = 0; i < stars.length; i++ )
{
int points = 4 + random.nextInt(3); // From 4 to 6 points.
double spin = random.nextDouble()*Math.PI/points; // Starting angle.
int size = 20 + random.nextInt(10); // Slight variation in size.
int x = random.nextInt(300);
int y = random.nextInt(300);
Color color = colors[random.nextInt(colors.length)];
if( traditional )
stars[i] = new TraditionalStar( x, y, size, points, color, spin );
else
stars[i] = new Star( x, y, size, points, color );
}
setBackground (Color.black);
}
public void paint(Graphics page)
{
for( int i = 0; i < stars.length; i++ )
stars[i].draw(page);
}
}
/**
* This class draws very simple, spiky "stars," really just
* lines radiating out from a central point.
*/
class Star
{
int centerX;
int centerY;
int radius;
Color color;
int totalLines;
public Star( int centerX, int centerY, int radius,
int totalLines, Color shade)
{
this.centerX = centerX ;
this.centerY = centerY ;
this.radius = radius ;
this.totalLines = totalLines ;
color = shade;
}
public Star (int centerX, int centerY, int radius, Color shade)
{
// Default the total lines to 6
this(centerX, centerY, radius, 6, shade);
}
public Star (int center, int radius, Color shade)
{
// centerX and centerY are same
this(center, center, radius, 6, shade);
}
public void draw(Graphics page)
{
double angle = 2*Math.PI/totalLines;
for (int i = 0; i < totalLines; i++)
{
int x1 = (int) (radius * Math.cos ( angle * i));
int y1 = (int) (radius * Math.sin ( angle * i));
x1 = centerX + x1;
y1 = centerY + y1;
page.setColor ( color ) ;
page.drawLine ( centerX, centerY, x1, y1);
}
}
}
/**
* This class draws more traditional stars, without the internal lines.
*/
class TraditionalStar extends Star
{
private double spin;
public TraditionalStar( int centerX, int centerY, int radius,
int totalLines, Color shade, double spin)
{
super(centerX, centerY, radius, totalLines, shade);
}
public void draw(Graphics page)
{
double pi = Math.PI;
double angle = 2*Math.PI/totalLines; // In radians, of course.
for( int i = 0; i < totalLines; i++ )
{
page.setColor( color );
int x = (int)(radius * Math.cos(spin + angle * i)) + centerX;
int y = (int)(radius * Math.sin(spin + angle * i)) + centerY;
int innerX = (int)(radius/2*Math.cos(spin + angle*i - angle/2)) + centerX;
int innerY = (int)(radius/2*Math.sin(spin + angle*i - angle/2)) + centerY;
page.drawLine( innerX, innerY, x, y );
innerX = (int)(radius/2*Math.cos(spin + angle*i + angle/2)) + centerX;
innerY = (int)(radius/2*Math.sin(spin + angle*i + angle/2)) + centerY;
page.drawLine( innerX, innerY, x, y );
}
}
}
|
|