如何在Processing中更改数组中每个椭圆的颜色?

时间:2014-07-17 18:51:35

标签: colors processing orbit

我试图弄清楚如何改变中心椭圆周围的三个椭圆的颜色。目前它们都是红色的,但我希望有一个是绿色的,一个是红色的,一个是黄色的。谁能想到办法呢?

这是我的处理代码:

class OrbitalBody {

float ox, oy, x, y, m, a;

OrbitalBody(float originX, float originY, float startX, float startY, float mass, float speed) {
        ox = originX;
        oy = originY;
        x = startX;
        y = startY;
        m = mass;
        a = speed / 200;
}

void update() {
// creates the orbit;

float nx = (x-ox) * cos(a) - (y-oy) * sin(a),
      ny = (x-ox) * sin(a) + (y-oy) * cos(a);
      x = nx+ox;
          y = ny+oy;
}

void draw() {
// defines the color of the ellipses, now:red;
fill(255,0,0);
ellipse(x,y,m,m);
}
}

ArrayList<OrbitalBody> bodies = new ArrayList<OrbitalBody>();
int mx, my;


void setup() {
size(420, 420);
mx = width/2;
my = height/2;


// distances([3]bodies-(mx,my) is equal. Meaning all three bodies rotate on the same orbit, value now: mx-100;

fill(0,255,0);
bodies.add(new OrbitalBody(mx, my, mx-100, my, 10, 10));
fill(0);
bodies.add(new OrbitalBody(mx, my, mx-100, my, 20, 3));
bodies.add(new OrbitalBody(mx, my, mx-100, my, 5, 13));

ellipseMode(CENTER);
}

void draw() {

// defines background color, now:white;
background(255);
noStroke();

// defines center cercle color value, now:pink;
// second value:alpha channel;
fill(#ec5b94,80); 

// mx and my are the coordinates of the center pink cercle;
ellipse(mx,my,50,50);
for(OrbitalBody b: bodies) {
          b.update();
          b.draw();
}
}

1 个答案:

答案 0 :(得分:1)

也许您可以使用颜色参数实例化每个OrbitalBody,然后在OrbitalBody的绘图方法中使用该参数。

基本上,这个类看起来像:

class OrbitalBody {

    float ox, oy, x, y, m, a;
    color c;

    OrbitalBody(float originX, float originY, float startX, float startY, float mass, float speed, color bodyColor) {
        // Do stuff
        c = bodyColor;
    }

    void draw() {
        fill(c);
        ellipse(x,y,m,m);
    }
}