Results 1 to 9 of 9
  1. #1
    encyclopedia is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Question Java question - arrays

    hi

    .Please help with this java array creation problem?
    for (int i = 0; i < 150; i++) {
    radius = r.nextInt(10) + 1;
    point = r.nextInt(3) + 4;
    x = r.nextInt(1300) + 5;
    y = r.nextInt(800) + 5;
    s = new Star(point, radius, x, y);
    s.draw(page);
    //stars[i] = new Star(point, radius, x, y);
    //stars[i].draw(page);
    stars[i] = s;

    System.out.println(i + "-> " + stars[i].getRadius() + "," + stars[i].getX() +
    "," + stars[i].getY() + "," + stars[i].getPt());

    System.out.println(0 + "-> " + stars[0].getRadius() + "," + stars[0].getX() +
    "," + stars[0].getY() + "," + stars[0].getPt());
    }

    I am trying to create an array containing 150 'star' objects using stars[i] = s. However this is not working as when i ask it to print the element at the 1st index(2nd print statement) it always prints the latest element pushed into the Star array. Basically the two print statements output the same result at each iteration.

    I think the problem is at the line where it says stars[i] = s. Please help me out.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Good debugging prints; your code makes sense: each element stars[i] refers to another Star object so my guess is that there's something rotten in your Star class itself; are the members x, y, etc. static by any chance?

    kind regards,

    Jos

  3. #3
    encyclopedia is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks jos for the comments...yes x, y radius and point have been declared as static...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by encyclopedia View Post
    thanks jos for the comments...yes x, y radius and point have been declared as static...
    That's the problem: because x and y (and some other members?) are static they have the same value for all Star objects. Remove the 'static' qualifier and you're in business.

    kind regards,

    Jos

  5. #5
    encyclopedia is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    unfortunately its still not working. Actually i am trying to make an applet in which simulating the sky at night( with twinkling stars). I am randomly choosing 40 stars in the sky class. But i think the problem lies at the array creation. somehow the object star is getting copied to all the array locations upto the current one.

    // star.java
    import java.awt.*;

    public class Star {

    private static int point;
    private static int radius;
    private static int xpos;
    private static int ypos;

    public Star(int pts, int rad, int x, int y) {
    point = pts;
    radius = rad;
    xpos = x;
    ypos = y;
    }

    public void draw(Graphics g) {
    g.setColor(Color.white);
    double angleR = ((360/point)*(Math.PI/180));
    for (double i = 0; i < 2 * Math.PI; i += angleR) {
    double cosV = (Math.cos(i));
    double sinV = (Math.sin(i));
    int xcoord = (int) (xpos + (radius * cosV));
    int ycoord = (int) (ypos + (radius * sinV));
    g.drawLine(xpos, ypos, xcoord, ycoord);
    }

    }

    public int getPt() {
    return point;
    }

    public int getX() {
    return xpos;
    }

    public int getY() {
    return ypos;
    }

    public int getRadius() {
    return radius;
    }

    }


    // sky.java


    import java.applet.*;
    import java.awt.*;
    import java.util.*;

    public class Sky extends Applet {

    private static final long serialVersionUID = 1L;

    int radius, point, x, y;
    Star s;

    public void paint(Graphics page) {
    Random r = new Random();
    setBackground(Color.black);

    Star [] stars = new Star[150];
    Star [] randomStars = new Star[40];


    // create and store star objects in the array
    for (int i = 0; i < 150; i++) {
    radius = r.nextInt(10) + 1;
    point = r.nextInt(3) + 4;
    x = r.nextInt(1300) + 5;
    y = r.nextInt(800) + 5;
    s = new Star(point, radius, x, y);
    s.draw(page);
    //stars[i] = new Star(point, radius, x, y);
    //stars[i].draw(page);
    stars[i] = s;
    System.out.println(i + "-> " + stars[i].getRadius() + "," + stars[i].getX() +
    "," + stars[i].getY() + "," + stars[i].getPt());
    System.out.println(0 + "-> " + stars[0].getRadius() + "," + stars[0].getX() +
    "," + stars[0].getY() + "," + stars[0].getPt());
    }

    for (int q = 0; q < stars.length; q++ ) {
    System.out.println(q + "-> " + stars[q].getRadius() + "," + stars[q].getX() +
    "," + stars[q].getY() + "," + stars[q].getPt());
    }



    // get random star objects
    for (int j = 0; j < 40; j++) {
    int index = r.nextInt(150);
    randomStars[j] = stars[index];
    System.out.println(index + "," + randomStars[j].getRadius() + "," + randomStars[j].getX() +
    "," + randomStars[j].getY() + "," + randomStars[j].getPt());
    }

    // switch off
    for (int k = 0; k < randomStars.length; k++) {
    int x = randomStars[k].getX() - randomStars[k].getRadius();
    int y = randomStars[k].getY() - randomStars[k].getRadius();
    page.setColor(Color.black);
    page.fillOval(x, y, randomStars[k].getRadius(), randomStars[k].getRadius());
    }
    waiting(1000);

    //switch on
    for (int k = 0; k < randomStars.length; k++) {
    s = randomStars[k];
    s.draw(page);
    }
    }

    private static void waiting(int n) {
    long t1, t2;
    t1 = System.currentTimeMillis();
    do {
    t2 = System.currentTimeMillis();
    }
    while (t2 - t1 < n);
    }


    }


    thanks in advance

  6. #6
    encyclopedia is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    i think its working now, i have removed the static keyword from the star class as well and getting the results as expected...guess u would have suggested that as well.!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by encyclopedia View Post
    Java Code:
    	private static int point;
    	private static int radius;
    	private static int xpos;
    	private static int ypos;
    Why are those members still static?

    kind regards,

    Jos

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by encyclopedia View Post
    i think its working now, i have removed the static keyword from the star class as well and getting the results as expected...guess u would have suggested that as well.!
    Yes I did; in all my replies as a matter of fact.

    kind regards,

    Jos

  9. #9
    encyclopedia is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    yay its working now!!! thanks!

    import java.awt.*;

    public class Star {

    int point, radius, xpos, ypos;

    public Star(int pts, int rad, int x, int y) {
    point = pts;
    radius = rad;
    xpos = x;
    ypos = y;
    }

    public void draw(Graphics g) {
    g.setColor(Color.white);
    double angleR = ((360/point)*(Math.PI/180));
    for (double i = 0; i < 2 * Math.PI; i += angleR) {
    double cosV = (Math.cos(i));
    double sinV = (Math.sin(i));
    int xcoord = (int) (xpos + (radius * cosV));
    int ycoord = (int) (ypos + (radius * sinV));
    g.drawLine(xpos, ypos, xcoord, ycoord);
    }

    }

    public int getPt() {
    return point;
    }

    public int getX() {
    return xpos;
    }

    public int getY() {
    return ypos;
    }

    public int getRadius() {
    return radius;
    }

    }




    import java.applet.*;
    import java.awt.*;
    import java.util.*;

    public class Sky extends Applet {

    private static final long serialVersionUID = 1L;

    int radius, point, x, y;
    Star s;

    public void paint(Graphics page) {
    Random r = new Random();
    setBackground(Color.black);
    Star [] stars = new Star[250];
    Star [] randomStars = new Star[70];
    // create and store star objects in the array
    for (int i = 0; i < 150; i++) {
    radius = r.nextInt(10) + 1;
    point = r.nextInt(3) + 4;
    x = r.nextInt(1300) + 5;
    y = r.nextInt(800) + 5;
    s = new Star(point, radius, x, y);
    s.draw(page);
    stars[i] = s;
    }
    // get random star objects
    for (int j = 0; j < 70; j++) {
    int index = r.nextInt(150);
    randomStars[j] = stars[index];
    System.out.println(index + "-> " + randomStars[j].getRadius() + "," + randomStars[j].getX() +
    "," + randomStars[j].getY() + "," + randomStars[j].getPt());
    }
    waiting(1200);
    // switch off
    for (int k = 0; k < randomStars.length; k++) {
    int x = randomStars[k].getX() - randomStars[k].getRadius();
    int y = randomStars[k].getY() - randomStars[k].getRadius();
    page.setColor(Color.black);
    page.fillRect(x, y, 2 * randomStars[k].getRadius() + 5, 2 * randomStars[k].getRadius() + 5);
    }
    waiting(1200);
    //switch on
    for (int k = 0; k < randomStars.length; k++) {
    s = randomStars[k];
    s.draw(page);
    }

    }

    private static void waiting(int n) {
    long t1, t2;
    t1 = System.currentTimeMillis();
    do {
    t2 = System.currentTimeMillis();
    }
    while (t2 - t1 < n);
    }


    }

Similar Threads

  1. Java arrays (not sure if this question is advanced)
    By aga010607 in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 02:43 PM
  2. Question about 2D arrays
    By fanle in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 06:52 PM
  3. Byte arrays and MIDI - simple question?
    By Ravaa in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 10:47 PM
  4. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 03:29 AM
  5. Question about arrays
    By nhlfan in forum New To Java
    Replies: 4
    Last Post: 11-15-2007, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •