The Artima Developer Community
Sponsored Link

Java Answers Forum
Stars Applet

5 replies on 1 page. Most recent reply: Mar 3, 2002 12:38 PM by Patti

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Stars Applet Posted: Feb 27, 2002 2:20 PM
Reply to this message Reply
Advertisement
Hi. I'm supposed to write an applet with a constructor called Star to create various size and color stars across a black sky. Stars are to be made like asterisks. I'm afraid I'm missing a lot here. This is what I've got. Any and all help appreciated.

import Star;
import java.awt.*;
import java.applet.Applet;


public class LineUp extends Applet {

private final int APPLET_WIDTH = 400;
private final int APPLET_HEIGHT = 150;
private final int RADIUS_MIN = 10;
private final int VARIANCE = 50;

private Star star1, star2, star3, star4;

public void init ()

{
int r1,r2,r3,r4; //radii of stars

r1 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;
r2 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;
r3 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;
r4 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;

star1 = new Star (50, Color.red, r1);
star2 = new Star (70, Color.yellow, r2);
star3 = new Star (125, Color.green, r3);
star4 = new Star (150, Color.blue, r4);

setBackground (Color.black);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
public void paint(Graphics page) {

star1.draw (page);
star2.draw (page);
star3.draw (page);
star4.draw (page);
}
}



Here's the partial consturctor:

import java.awt.*;

public class Star {

private int x;
private int y;
private Color color;
private int size;
private int crosses;

public Star (int centerX, int centerY, Color shade, int radius)
{

x = centerX;
y = centerY;
color = shade;
size = radius;
}

public void draw (Graphics page)
{
page.setColor (color);

page.drawLine (x - 10, y - 10, 20, 50);
page.drawline (x -5, y -15,
}

}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Stars Applet Posted: Mar 2, 2002 7:43 PM
Reply to this message Reply

import java.awt.*;
import java.applet.Applet;


public class LineUp extends Applet {

private final int APPLET_WIDTH = 400;
private final int APPLET_HEIGHT = 150;
private final int RADIUS_MIN = 10;
private final int VARIANCE = 50;

private Star star1, star2, star3, star4;

public void init (){
int r1,r2,r3,r4; //radii of stars

r1 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;
r2 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;
r3 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;
r4 = (int) (Math.random() * VARIANCE) + RADIUS_MIN;

star1 = new Star (50,75, Color.red, r1);
star2 = new Star (70,75, Color.yellow, r2);
star3 = new Star (125,75, Color.green, r3);
star4 = new Star (150,75, Color.blue, r4);

setBackground (Color.black);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}

public void paint(Graphics page) {

star1.draw (page);
star2.draw (page);
star3.draw (page);
star4.draw (page);
}
}





import java.awt.*;
 
public class Star {
 
	private int x;
	private int y;
	private Color color;
	private int size;
	private int crosses;
 
	public Star (int centerX, int centerY, Color shade, int radius){
 
		x = centerX;
		y = centerY;
		color = shade;
		size = radius;
	}
 
	public void draw (Graphics page){
		page.setColor (color);
 
		page.drawLine (x - size, y + size, x, y - size);
		page.drawLine (x, y - size, x + size, y + size); 
		page.drawLine (x + size, y + size, x - size, y - size/2);
		page.drawLine (x - size, y - size/2, x + size, y - size/2); 
		page.drawLine (x + size, y - size/2,x - size, y + size); 
	}
 
} 



<html>
<head>
<title>LineUp</title>
</head>
&l t;body>
<applet code="LineUp.class" width ="400" height = "150">

</applet>
</body>

</html>

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Stars Applet Posted: Mar 3, 2002 8:48 AM
Reply to this message Reply
Thank you so much !!!

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Stars Applet Posted: Mar 3, 2002 9:04 AM
Reply to this message Reply
It was fun.

Charles

charbell@bellsouth.net

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Seeing Stars Posted: Mar 3, 2002 11:07 AM
Reply to this message Reply
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 );
        }
    }
}

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Seeing Stars Posted: Mar 3, 2002 12:38 PM
Reply to this message Reply
You guys have been wonderful !! Thanks so much !!

Flat View: This topic has 5 replies on 1 page
Topic: doubly linked list Previous Topic   Next Topic Topic: java.io.FilePermission

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use