У меня есть ArrayList
, и я хочу сохранить несколько планет из Солнечной системы в виде фигур в этом ArrayList
, но в конце сохраняется только последний параметр объекта, так как всего объектов существует.
Это в основном:
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;
}
}
И это 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);
}
На /*1*/
название планет кажется хорошим, но на /*2*/
и /*3*/
каждая планета называется «Плутон».Зачем?Как я могу решить эту проблему?