I am missing the way to call x,y coordinates and utilize the radius setting in the Star constructor of my applet to display various stars across a sky. I know I shouldn't have the drawlines the way they are. Please help.
star1 = new Star (50, 30, Color.red, r1); star2 = new Star (70, 20, Color.yellow, r2); star3 = new Star (125, 45,Color.green, r3); star4 = new Star (150, 110, Color.blue, r4); star5 = new Star (30, 40, Color.cyan, r1); star6 = new Star (225, 110, Color.red, r4); star7 = new Star (210,50, Color.yellow, r3); star8 = new Star (300, 140, Color.magenta, r4); star9 = new Star (140, 70, Color.cyan, r4); star10 = new Star (150, 90, Color.blue, r2);
Hi I have created a drawStar method for the applet. This method takes arguments 1. The Graphics object 2. The center of the star 3. The radius of the star 4. No of lines you want in the star In paint method, I am just drawing some stars, for example. I am using cos and sin functions of Math class to compute the other end of the lines to be drawn. I think you need to use drawStar() method in your SkyNight class and then you will get the desired result. If you need further assistance in drawing your the stars let me know. Thanks KIshori /////////////Sky.java///////// import java.awt.*; import java.applet.Applet;
public class Sky extends Applet {
public void init () { }
public void paint(Graphics page) { for ( int x = 3; x < 25; x++ ) { drawStar ( page, x * 15, 10, x ) ; } }
public void drawStar ( Graphics g, int center, int radius, int totalLines ) { // First divide the circle in equal no of sectors depening on no of lines if ( totalLines < 3 ) { totalLines = 4 ; }
if ( radius < 5 ) { radius = 5 ; }
double angle = 2 * Math.PI / totalLines ;
// Calculate x, y co-ordinates for each lines. // Note that x1, y1 will be same as center. We will caculate only x2 and y2
for ( int i = 0 ; i < totalLines; i++ ) { int x1 = (int)( radius * Math.cos ( angle * i ) ) ; int y1 = (int)( radius * Math.sin ( angle * i ) ) ;
// Since x1 and y1 are relative to center, convert them to absolute values x1 = center + x1 ; y1 = center + y1 ;
// Draw the lines g.drawLine ( center, center, x1, y1 ) ; }
Kishori, I hate to bother you again, especially since I'm just a beginner, but my teacher wants us to write this thing HER WAY. I tried to move things around, but I'm just wasting hours and hours ont this. She wants: Create a class called Star that represents a graphical depiction of a star-lines radiating from a center point. Let the constructor of the star accept the number of points in the star (4, 5, or 6), the radius of the star, the x,y coordiantes of the center of the star. Ths class will have one method called draw, which will draw the star onto a graphics object called page.. Then write an applet that draws a night full of stars (at least 10) of various sizes and in various positions. Your way is much better, but I've got to switch it to her way. Here's what I've got at this point. Can you fix it or can you give me a hint?
private Star star1,star2,star3,star4,star5,star6,star7,star8,star9,star10;
public void init (){ star1 = new Star (60,60, Color.red); star2 = new Star (30,75, Color.yellow); star3 = new Star (80,30, Color.green); star4 = new Star (120,40, Color.cyan); star5 = new Star (200,80, Color.yellow); star6 = new Star (250,130, Color.red); star7 = new Star (90,100, Color.cyan); star8 = new Star (100,110, Color.magenta); star9 = new Star (240,30, Color.green); star10 = new Star (275,65, Color.blue);
public Star (int center, Color shade, int radius, int totalLines) { int x1 = (int) (radius * Math.cos ( angle * i)); int y1 = (int) (radius * Math.sin (angle * i)); int radius = 10; baseX = center; color = shade;
} public void draw (Graphics page){
double angle = 2*Math.PI/totalLines;
for (int i = 0; i < totalLines; i++){
x1 = center + x1; y1 = center + y1; g.drawLine (center,center,x1,y1);
Hello This time it works yours teacher's way. You may want to create some more constructors for Star class so that you can use some default values for radius and colors if not supplied, Thanks Kishori ///////////NightSky.java//////////// import java.awt.*; import java.applet.Applet;
public class NightSky extends Applet {
private Star star1,star2,star3,star4,star5,star6,star7,star8,star9,star10;
public void init (){ star1 = new Star (60,60, 10, Color.red); star2 = new Star (30,30,10, Color.yellow); star3 = new Star (80,80, 10, Color.green); star4 = new Star (120, 120, 10, Color.cyan); star5 = new Star (200,200, 10, Color.yellow); star6 = new Star (250,250, 10, Color.red); star7 = new Star (90,90, 10, Color.cyan); star8 = new Star (100,100, 10, Color.magenta); star9 = new Star (240,240, 10, Color.green); star10 = new Star (275,275, 10, Color.blue);
private int centerX; private int centerY; private int radius ; private Color color; private 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));
I put it all together. I get a recursive constructor invocation error on the "this" at here below: public Star (int centerX, int centerY, int radius, Color shade){ this(here)(centerX, centerY, radius, 6, shade);
What does that mean? I know you are REALLY SICK of me. I wish there was some way I could thank you.
I don't see any problems with recursive constructors, maybe the code you have is slightly different than what is posted? I could see how such a problem could occur if you are not careful with several variations of multi-parameter constructors that are calling others.
Anyhow, I did see a few other problems, like having ten star variables instead of an array of them, having hard-coded star positions and sizes and only displaying 6-sided stars. Here is a modified version of the last post. This one can also run as a stand-alone application (run with java on the command line), by the way. Using the array of stars makes things much more flexible, as you can see, since you can now specify the number of stars to display on the command line, as well (e.g. "java NightSky 33").
///////////NightSky.java////////////
import java.awt.*;
import java.applet.Applet;
import java.util.Random;
publicclass NightSky extends Applet
{
publicstaticvoid main(String[] args)
{
Applet applet = null;
if( args.length > 0 )
try
{
applet = new NightSky( Integer.parseInt(args[0]) );
}
catch( NumberFormatException nfe )
{
System.out.println( args[0] +
" doesn't grok as a number, proceeding " +
"with defaults..." );
}
if( applet == null )
applet = new NightSky();
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);
}
privateint starCount = 50;
private Star [] stars;
privatestaticfinal Random random = new Random();
privatestatic Color[] colors = { Color.red, Color.yellow, Color.green,
Color.cyan, Color.magenta, Color.blue };
public NightSky()
{
}
public NightSky( int starCount )
{
this.starCount = starCount;
}
publicvoid init ()
{
stars = new Star[starCount];
for( int i = 0; i < stars.length; i++ )
stars[i] = new Star( random.nextInt(300),
random.nextInt(300),
10 + random.nextInt(5), // Slight variation in size.
4 + random.nextInt(3),
colors[random.nextInt(colors.length)] );
setBackground (Color.black);
}
publicvoid paint(Graphics page)
{
for( int i = 0; i < stars.length; i++ )
stars[i].draw(page);
}
}
class Star
{
privateint centerX;
privateint centerY;
privateint radius ;
private Color color;
privateint 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) ;
}
publicvoid 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);
}
}
}
Patti, I think you have either modified the code and then made some error or while copying from this page you missed some code. I copied the same code I posted and it works fine. I have just given you hint on how to draw a star. You can use it in any way that fits your requirement. Thanks Kishori