Rectangle outlines causing trouble inside the draw() loop

edited November 2016 in Questions about Code

Basically when I compile my sketch it first looks and works the way it's intended (Grid of rectangles in different shades of grey, whilst mousing over one it turns black). But after a bit the entire sketch gets kind of messed up and you can see lines going from (0, 0) to the respective rectangle coordinates. I've been trying to fix it for a while and so far I've noticed that it doesn't happen when the program isn't inside the draw() function (but mouse input obviously doesn't work then, it's just a static image). Also when I change the stroke colour before each rectangle is drawn it changes the colour of the outlines going crazy as well. Tried googling but couldn't find anything, also pretty new to processing.

tl;dr: tried programming a grid of colored rectangles that change colour trough mouse input, ended up with loads of lines going haywire.

Help appreciated!

int maxRows = 10;
int maxColumns = 10;
int[][] colorValue = new int[maxRows][maxColumns];
float fieldSize = 50;

void setup() {
  size(500, 500);
  for (int row = 0; row < maxRows; row++) {
    for (int column = 0; column < maxColumns; column++) {
      colorValue[row][column] = int(random(255));
    }
  }
}

void draw() {
  int y = 0;
  for (int row = 0; row < maxRows; row++) {
    int x = 0;
    for (int column = 0; column < maxColumns; column++) {
      if (mouseX > x && mouseX < (x + fieldSize) && mouseY > y && mouseY < (y + fieldSize)) {
        fill(0);
        rect(x, y, fieldSize, fieldSize);
      } else {
        fill(colorValue[row][column]);
        rect(x, y, fieldSize, fieldSize);
        x += 50;
      }
    }
    y += 50;
  }
}

Here's what it looks like (sorry, couldn't get the image option to work properly at all) http://www.imgur.com/a/0T36k

Answers

Sign In or Register to comment.