我有一个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;
在/*1*/
行星的名字似乎不错,但/*2*/
在/*3*/
每颗行星的名字是“冥王星”。为什么?我该如何解决这个问题?
陪伴而非守候
紫衣仙女
相关分类