0

I am trying to draw a 4x4 grid using a 2D array. When I run the code, I only draw a square in the lower left corner. I think the issue might be with my y-coordinate but I am not completely sure.

StdDraw.setScale(0,4);

int[][] grid = new int[4][4];

for (int x = 0; x < grid.length; x++)
{
  for (int y = 0; y < grid[0].length; y++)
  {
    StdDraw.setPenColor(StdDraw.BLUE);
    StdDraw.filledSquare(grid[x][y], grid[x+1][y+1], 1);
  }
}
inda1
  • 63
  • 1
  • 10

2 Answers2

3

Isn't this just because your multidimensional grid array is all 0's due to default initialization? You are drawing four squares at coord 0,0 with size 1.

Barry
  • 237
  • 1
  • 2
  • 11
1

I got this to work:

StdDraw.setScale(0,4);

int[][] grid = new int [4][4];

for (int x = 0; x < grid.length; x++)
{
  for (int y = 0; y < grid.length; y++)
  {
    grid[x][y] = 255;
  }
}

for (int x = 0; x < grid.length; x++)
{
  for (int y = 0; y <grid.length; y++)
  {
    StdDraw.square(x, y, 1);
  }
}
inda1
  • 63
  • 1
  • 10
  • Good work, inda1. If Barry's answer helped you solve your problem (I think it did), consider marking his answer as correct, or upvote it or something. – DavidS Apr 12 '16 at 18:36