Using arrays of objects

edited September 2014 in Questions about Code

Problem solved - see Thread: Questions about Code - "Arrays in a Array"

Thanks - now i know what a 2D array is..


hi there processing forum members, i recently startet to evole my first programming skills in processing. it's going pretty good so far, and i've covered myself in some beginner guides.

my question is about objects, arrays and iteration:

i have created a rectangle object, which turns black when mouseX and mouseY is within its boarders. after leaving the area with mouseX or mouseY, the rectangle is fading to white. i have tried to create a "lawn" of those rectangles over the entire sketch. but it doesnt really work the way i want it. anybody who can help me with arrays of objects?? when i look at this code, it is obvious that the redundant part can be improved, but how? :)

thanks a lot and best, markus

int numRect = 4;            // is it possible to make an array of an array ???
Rectangle[] rects1 = new Rectangle[numRect];
Rectangle[] rects2 = new Rectangle[numRect];
Rectangle[] rects3 = new Rectangle[numRect];
Rectangle[] rects4 = new Rectangle[numRect];


void setup () {
  size(500, 500);
  smooth();
  background(255);
  for (int i = 0; i < rects1.length; i++) {                
    float x = width/numRect * i;
    float y = height/numRect * 0;
    rects1[i] = new Rectangle(x, y, width/numRect, height/numRect);
  }
  for (int j = 0; j < rects2.length; j++) {
    float x = width/numRect * j;
    float y = height/numRect * 1;
    rects2[j] = new Rectangle(x, y, width/numRect, height/numRect);
  }
  for (int k = 0; k < rects2.length; k++) {
    float x = width/numRect * k;
    float y = height/numRect * 2;
    rects3[k] = new Rectangle(x, y, width/numRect, height/numRect);
  }  
  for (int l = 0; l < rects2.length; l++) {
    float x = width/numRect * l;
    float y = height/numRect * 3;
    rects4[l] = new Rectangle(x, y, width/numRect, height/numRect);
  }
}


void draw () {
  for (int i = 0; i < rects1.length; i++) {
    rects1[i].display();
  }
  for (int j = 0; j < rects2.length; j++) {
    rects2[j].display();
  }
  for (int k = 0; k < rects1.length; k++) {
    rects3[k].display();
  }
  for (int l = 0; l < rects2.length; l++) {
    rects4[l].display();
  }
}



class Rectangle {
  float x;
  float y;
  float w;
  float h;
  float colorFade;
  float fadeSpeed;

  Rectangle(float x_, float y_, float w_, float h_) {
    rectMode(CORNER);
    noStroke();
    x = x_;
    y = y_;
    w = w_;
    h = h_;
    colorFade = 0;
    fadeSpeed = 15;
  }

  void display () {
    if (mouseX > x && mouseY > y && mouseX < x+w && mouseY < y+h) {
      colorFade = 0;
      fill(colorFade);
      rect(x, y, w, h, w/20);
    } else if (mouseX < x || mouseY < y || mouseX > x+w || mouseY > y+h) {
      colorFade = colorFade + fadeSpeed;
      fill(colorFade);
      rect(x, y, w, h, w/20);
    }
  }
}

Answers

  • edited September 2014 Answer ✓

    Problem solved - see Thread: Questions about Code - "Arrays in a Array"

    Thanks :D - now i know what a 2D array is..

Sign In or Register to comment.