The Artima Developer Community
Sponsored Link

Java Answers Forum
Please Kishori Help

6 replies on 1 page. Most recent reply: Mar 1, 2002 6:10 AM by Kishori Sharan

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 6 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Please Kishori Help Posted: Feb 28, 2002 11:18 AM
Reply to this message Reply
Advertisement
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.

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


public class NightSky extends Applet {


private final int RADIUS_MIN = 10;
private final int VARIANCE = 30;

private Star star1, star2, star3, star4, star5, star6, star7, star8, star9, star10;

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, 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);


setBackground (Color.black);


}

public void paint(Graphics page) {

star1.draw4 (page);
star2.draw5 (page);
star3.draw5 (page);
star4.draw4 (page);
star5.draw5 (page);
star6.draw6 (page);
star7.draw6 (page);
star8.draw4 (page);
star9.draw6 (page);
star10.draw5 (page);


}
}

import java.awt.*;

public class Star {

private int x;
private int y;
private Color color;
private int radius;


public Star (int center, int bottom, Color shade, int size)
{


color = shade;
radius = size;

}
public void draw4 (Graphics page)
{

int size = radius;
page.setColor (color);
page.drawLine (35,30,55,30);
page.drawLine (45,15,45,45);


}
public void draw5 (Graphics page)
{
int size = radius;
page.setColor (color);
page.drawLine (60,60,75,55);
page.drawLine (60,60,75,70);
page.drawLine (60,60,60,50);
page.drawLine (60,60,55,75);
page.drawLine (60,60,50,60);


}
public void draw6 (Graphics page)
{
int size = radius;
page.setColor (color);
page.drawLine (90,90,90,70);
page.drawLine (90,90,100,80);
page.drawLine (90,90,100,95);
page.drawLine (90,90,80,80);
page.drawLine (90,90,80,95);
page.drawLine (90,90,90,110);


}
}


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

You can draw your starts now Posted: Feb 28, 2002 2:24 PM
Reply to this message Reply
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 ) ;
}

}
}

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: You can draw your stars now Posted: Feb 28, 2002 5:49 PM
Reply to this message Reply
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?

import Star;
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, 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);

setBackground (Color.black);
}


public void paint(Graphics page) {

star1.draw (page);
star2.draw (page);
star3.draw (page);
star4.draw (page);
star5.draw (page);
star6.draw (page);
star7.draw (page);
star8.draw (page);
star9.draw (page);
star10.draw (page);


}
}


import java.awt.*;

public class Star {

private int baseX;
private Color color;


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

}

}

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Yours teacher's way Posted: Feb 28, 2002 7:04 PM
Reply to this message Reply
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);

setBackground (Color.black);
}


public void paint(Graphics page) {
star1.draw (page);
star2.draw (page);
star3.draw (page);
star4.draw (page);
star5.draw (page);
star6.draw (page);
star7.draw (page);
star8.draw (page);
star9.draw (page);
star10.draw (page);

}
}




class Star {

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

x1 = centerX + x1;
y1 = centerY + y1;
page.setColor ( color ) ;
page.drawLine ( centerX, centerY, x1, y1);
}
}

}

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Yours teacher's way Posted: Feb 28, 2002 8:40 PM
Reply to this message Reply
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.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Yours teacher's way Posted: Feb 28, 2002 10:33 PM
Reply to this message Reply
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;
 
public class NightSky extends Applet 
{
    public static void 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);
    }
 
    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( int starCount )
    {
        this.starCount = starCount;
    }
 
    public void 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);
    }
    
    
    public void paint(Graphics page) 
    {
        for( int i = 0; i < stars.length; i++ )
            stars[i].draw(page);
    }
}
 
class Star 
{
 
    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));
            
            x1 = centerX + x1;
            y1 = centerY + y1;
            page.setColor ( color ) ;
            page.drawLine ( centerX, centerY, x1, y1);
        }
    }
}

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Yours teacher's way Posted: Mar 1, 2002 6:10 AM
Reply to this message Reply
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

Flat View: This topic has 6 replies on 1 page
Topic: Legacy Answers Forum Posts Are Live Previous Topic   Next Topic Topic: sorting thro' xslt

Sponsored Links



Google
  Web Artima.com   

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