Inheritance Class Objects in 2D Grid Array do not align

I’m trying to get a 2D grid to display properly with 1 subclass object before adding additional subclasses.

The code for this 2D array grid is pulled from one of my previous projects, and it worked fine.
However, the difference is: with this sketch I am breaking the class down into subclasses. I am thinking this may be the issue? However, not able to find any specific threads on-line to pursue.
I also tried implementing pushMatrix() and popMatrix() in the subclass, to no avail…
Or, some basic oversight…

Any guidance most welcome!
:nerd_face:

// MAIN

HalfmoonRect [][] myHMrect;

int num = 4;
int cols = num, rows = cols;

void setup(){
  size (800, 800);
  
  frameRate (1);
  
  float x = 0, y = x, sz = width/num;
  
  myHMrect = new HalfmoonRect [cols][rows];
  for (int i = 0; i < cols; i++){
    for (int j = 0; j < rows; j++){
      myHMrect[i][j] = new HalfmoonRect (x+(i*width/cols), y+(j*height/rows), sz);
    }
  }
}
void draw(){
   
  for (int i = 0; i < cols; i++){
    for (int j = 0; j < rows; j++){
      myHMrect[i][j].display();
    }
  }
}
// SUPER

class Halfmoon {

  color c1 = #FFFFFF, 
    c2 = #BEBEBE, 
    c3 = #7F7F7F, 
    c4 = #3F3F3F, 
    c5 = #000000;
  color[] grays = {c1, c2, c3, c4, c5};  

  float deg, rad;
  float x, y, sz;

  Halfmoon(float tempX, float tempY, float tempSz) {    
    x = tempX;
    y = tempY;
    sz = tempSz;
  
    //deg = radians(90);
  }

void rectPlusColor (color rectColor) {
    fill (rectColor);
    rectMode (CENTER);
    rect (x, y, sz, sz);
  }
}

// SUBCLASS

class HalfmoonRect extends Halfmoon {

  HalfmoonRect(float x, float y, float sz) {
    super(x, y, sz);
  }

  void display() {
    noStroke();
    translate (100, 100);
    
    rectPlusColor(grays[int(random(grays.length))]);
    
  }
}
1 Like

Could you post a picture of what you want vs. what you’re getting?

This is what I’m getting, rectangles are spread out. The code I posted reflects this same behavior.

What I want is a simple grid layout – 4 columns across, 4 rows down of rectangles.

Okay, so I was crawling through your code and found this:

translate (100, 100);

What is this line for?

Edit: Try commenting it out!

To offset rectangle position when I draw from from center – ie rectMode(CENTER) is also in the code.

Yes, i tried that already.

translate() is a cumulative function: You’re telling the computer to offset by (100,100), then (200,200), then (300,300), then…
It does, however, get reset after every draw-loop

Interesting, I just got it to work on my machine.

Can you clarify what you tried?

3 Likes

Oh, I see. Now the grid appears. But is entirely shifted to the left and up by 100. Entire grid as a unit needs to move right 100 and down 100…

HA!! Just changed the x position…
OK! Thank you @tony! I got it.

:nerd_face:

3 Likes