简体   繁体   English

绘制4x4网格Java

[英]Draw a 4x4 Grid Java

I am trying to draw a 4x4 grid using a 2D array. 我正在尝试使用2D数组绘制4x4网格。 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. 我认为问题可能出在我的y坐标上,但我不确定。

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);
  }
}

Isn't this just because your multidimensional grid array is all 0's due to default initialization? 这不仅仅是因为默认初始化导致多维网格数组全为0吗? You are drawing four squares at coord 0,0 with size 1. 您将在坐标0,0处绘制大小为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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM