I have an ArrayList and I want to save some planets from the solar system as shapes into this ArrayList, but at the end, only the last object parameter is saved as many objects are there in all.
This is in main:
ArrayList<Shape> shapes = new ArrayList<Shape>();
void setup() {
size(1600, 800);
generateSolarSystem();
}
void draw() {
update();
//background(255);
int begin_x = 100;
int begin_y = 100;
int distance = 1;
for (Shape s : shapes) {
pushMatrix();
translate(begin_x+distance, begin_y);
scale(1.1, 1.1);
s.Draw();
text(s.name, begin_x+distance, begin_y+10);
distance += 100;
System.out.println("name: " + s.name); /*3*/
popMatrix();
}
}
void generateSolarSystem() {
/**/
int d = 10;
/**/
Shape planet = new Circle();;
for(int idx = 0; idx<9; ++idx){
switch(idx) {
case 0:
//Mercury
planet.planet_color_r = 128;
planet.planet_color_g = 128;
planet.planet_color_b = 128;
planet.name = "Mercury";
planet.mass = "33011 x 10^23";
break;
case 1:
// Venus
planet.planet_color_r = 255;
planet.planet_color_g = 255;
planet.planet_color_b = 0;
planet.name = "Venus";
planet.mass = "4.8675 × 10^24 kg";
break;
case 2:
// Earth
planet.planet_color_r = 0;
planet.planet_color_g = 0;
planet.planet_color_b = 255;
planet.name = "Earth";
planet.mass = "4.8675 × 10^24 kg";
break;
case 3:
// Mars
planet.planet_color_r = 255;
planet.planet_color_g = 128;
planet.planet_color_b = 0;
planet.name = "Mars";
planet.mass = "4.8675 × 10^24 kg";
break;
case 4:
// Jupiter
planet.planet_color_r = 150;
planet.planet_color_g = 75;
planet.planet_color_b = 0;
planet.name = "Jupiter";
planet.mass = "4.8675 × 10^24 kg";
break;
case 5:
// Saturn
planet.planet_color_r = 147;
planet.planet_color_g = 131;
planet.planet_color_b = 105;
planet.name = "Saturn";
planet.mass = "4.8675 × 10^24 kg";
break;
case 6:
// Uranus
planet.planet_color_r = 140;
planet.planet_color_g = 205;
planet.planet_color_b = 216;
planet.name = "Uranus";
planet.mass = "4.8675 × 10^24 kg";
break;
case 7:
// Neptune
planet.planet_color_r = 53;
planet.planet_color_g = 110;
planet.planet_color_b = 163;
planet.name = "Neptune";
planet.mass = "4.8675 × 10^24 kg";
break;
case 8:
// Pluto
planet.planet_color_r = 194;
planet.planet_color_g = 196;
planet.planet_color_b = 168;
planet.name = "Pluto";
planet.mass = "4.8675 × 10^24 kg";
break;
}
shapes.add(planet);
/*1*/
text("added: " + planet.name, 10, d);
d += 10;
/**/
}
//check
d += 10;
for(Shape s : shapes) {
/*2*/
text("check - " + s.name, 10, d);
d += 10;
}
}
And this is a Shape:
abstract class Shape {
PVector position = new PVector();
PVector fill_color = new PVector(0, 0, 0);
PVector stroke_color = new PVector(0, 0, 0);
PVector select_fill_color = new PVector(255, 0, 0);
PVector select_stroke_color = new PVector(255, 0, 0);
Boolean selected = false;
int planet_color_r;
int planet_color_g;
int planet_color_b;
String name;
String mass;
int detailness = 10;
abstract void Draw();
abstract Boolean Contains(int x, int y);
}
At /*1*/ the name of planets is seems good, but at /*2*/ and at /*3*/ every planet name is "Pluto". Why? How can I resolve this?
Shape planet = new Circle();;inside for loop, removing double;, and instead++idxtry withidx++