Rectangle coding help

int numRows;
int rowColumns;
int rowLength;

int startX;
int startY;

int rectWidth;
int rectHeight;

boolean isColour;

void setup () {
  size (900, 400);
  stroke (0);
  numRows = 1;
  rowColumns = 1;
  rowLength = 10;
  startX = 50;
  startY = 350;
  rectWidth = 75;
  rectHeight = 30;
  isColour = false;
}

void draw() {
  background (155);
  
  while (numRows < rowLength) {
    rect (startX, startY, rectWidth, rectHeight); 
    startX = startX + rectWidth;
    numRows = numRows + 1; //adding this line breaks the code thus no rectangle is drawn??!)
    print(numRows + ", ");
  }
  
}

I want the rectangles to keep producing while numRows is less than rowLength. For some reason, after adding the numRows + 1; (i.e. the count), no rectangle is produced. What am I doing wrong here?

Thanks!
Before I add the numRows = numRows + 1 command

After I add the numRows = numRows + 1 command, there’s no rectangle being drawn

Hello,

Please format your code:
https://discourse.processing.org/faq#format-your-code

Is this a homework question or academic assignment?

You edited your last post to remove the code after it was solved:

:)

It’s a homework question. And apologies for that thread. I will not do that again.

Hello,

Your program is executing exactly the way you coded it. After the the first draw() loop (which has rectangles) the next loop falls through the while loop (10 is not < 10) and the canvas is cleared for each draw() cycle that follows.

I added a noLoop() to end of setup() so draw() only loops once and you can see output.
I also added some println() statements.

First cycle (frame) of draw():


After that background() clears canvas and it is grey.

Try initializing variables at start of draw before the while loop.

:)

1 Like

I understand now. Many Thanks!

1 Like