如何从具有不同变量的类创建多个对象?

时间:2015-12-01 18:43:43

标签: java arrays class object processing

我有一个名为Rain的班级。它有一个构造函数和各种方法。在我的程序中,我希望能够生成50个对象,但我希望它们沿x轴具有不同的值。我完成了2个物体,然后它们沿着该区域向下滑动,但我希望能够创建50多个这样的物体,这样我才能达到下雨的效果。基本上这是我的代码和我的构造函数。

雨计划:

Rain rain;
Rain rain2;

void setup()
{
    size (400,400);
    noStroke();
    rain = new Rain(20,random(0,10),3,15);
    rain2 = new Rain(random(15,35), random(70,110),3,15);
}

void draw()
{
    background(0);
    rain.colour(125,155,100);
    rain.display();
    rain2.colour(125,155,100);
    rain2.display();
}

以下是班级本身:

class Rain
{
    float wCoord, xCoord, yCoord, zCoord;
    int red, green, blue, gray;

    Rain()
    {
    }

    Rain(float wCoord, float xCoord, float yCoord, float zCoord)
    {
        this.wCoord = wCoord;
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        this.zCoord= zCoord;
    }

    void display()
    {
        rect(20,xCoord, yCoord, zCoord);
        xCoord+=3;
        if(xCoord>height-5)
            {
                xCoord=random(0,15);
            }
    }

    void colour(int red, int green, int blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
        fill (red, green, blue);
    }

    void colour(int gray){
        this.gray = gray;
        fill (this.gray);
    }
}

1 个答案:

答案 0 :(得分:0)

我在代码中做了一些更改。

查看评论

//an array of Rains... shouldn't it be called Drop
Rain[] drops = new Rain [1000];

void setup()
{
  size (400, 400);

  // initialize all Rains
  // the constructor gets no parmeters
  // and random initialization is handled
  // at class itself
  for (int i = 0; i < drops.length; i++) {
    drops[i] = new Rain();
  }
}

void draw()
{
  background(0);

  // just diaplay them
  for (Rain r : drops) {
    r.display();
  }
}


//any key to demosntrate colour() method
void keyPressed() {
  for (Rain r : drops) {
    r.colour(255);
  }
}

// and the overload version
void keyReleased() {
  for (Rain r : drops) {
    r.colour(125, 155, 100);
  }
}



class Rain
{    
  // you never used wcoord... got rid
  // renamed some vars to make code easier to read
  // like now the coord used to y position is 
  // yCoord instead of xCoord... :)
  // variables with meaningfull names rules

  float xCoord, yCoord, w, h, speed;

  // a default color
  color c = color(125, 155, 100);

  final static int MIN_Y      = -200, MAX_Y = 0;
  final static int MIN_WIDTH  = 2, MAX_WIDTH = 4;
  final static int MIN_HEIGHT = 5, MAX_HEIGHT = 13;
  final static int MIN_SPEED  = 2, MAX_SPEED = 6;

  Rain()
  {
    //Get new random values
    initialize();
  }


  void initialize() {
    yCoord = random(MIN_Y, MAX_Y);
    xCoord = random(width);
    w      = random(MIN_WIDTH, MAX_WIDTH);
    h      = random(MIN_HEIGHT, MAX_HEIGHT);
    speed  = random(MIN_SPEED, MAX_SPEED);
  }

  void display()
  {
    noStroke();
    fill(c);
    rect(xCoord, yCoord, w, h);

    yCoord+=speed;

    if (yCoord > height-5)
    {
      initialize();
    }
  }

  void colour(int red, int green, int blue)
  {

    c = color (red, green, blue);
  }

  void colour(color gray) {
    c = color (gray);
  }
}