# [java] Managing Thousands of Objects for a Planetarium

This topic is 3659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to write a software rendered planetarium. I have a Star.class that contains methods that will transform their coordinates from world space into view space, but I need to find a simpler method to create thousands of instances of this class and control them. Right now I am doing this to create the stars.
Star Polaris = new Star();
Star AlphaCentari = new Star();
Polaris.setLocation(430Ly);
AlphaCentari.setLocation(4Ly);
With this system I would have to enter thousands of lines just to set up all the instances of my Star.class. Is there a way that I can create the instances of these classes and control their methods using an array? This would allow me to store all the stars in a file and load it during runtime.

##### Share on other sites
Feed,

Just add the name of the star as a string to your star class. Then you can initialize an array of however many stars you'd like and read in the name and location from a file.

I'm not particularly familiar with Java so I'm not sure the exact syntax or Object/method names. In general however, you're going to do something like the following:

Open the File
Ask the file how many stars you have or compute it from the size of the file
Allocate memory for your array of stars
Iterate through the file, reading one star at a time
- Take the data from the file and set the values of the star at the current index
- increment the index
Close the file

If you've serialized your stars as in-memory objects, of if there's a class that allows you to do so, you could also read in the entire array of stars in one swoop and assign it to your pre-allocated array. Again, not being as familiar with Java I'm not sure if it has the functionality/library support to do this, however you could do this easily with any of the .NET languages.

Cheers and Good Luck!

##### Share on other sites
public class Star {  Star(string name, double distance)  {    this.name = name;    this.distance = distance;  }    void print()  {    System.out.println(name + " - " + distance);  }  string name;  double distance;};...List<Star> stars = new List<Star>();stars.add(new Star("Polaris", 430);stars.add(new Star("Alpha Centauri", 4);

foreach (Star s : stars) {
s.print();
}[/source]

Note that the above applies to Java 6 (perhaps 5) due to use of generics.

This makes it trivial to implement file I/O.

##### Share on other sites
Thank you guys. I am going to try those out.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×