2

Hey I am trying to draw a grid. I have dreated a 2D array and I am trying to fill it with Rectangle2D's. I would like the grid to be equal squares where a character can alk on. Here is my code:

public class GameWindow
{    
public static int[][] map = {
    {0, 0, 1, 0, 0},
    {0, 0, 1, 0, 0},
    {0, 0, 1, 0, 0},
    {0, 0, 1, 0, 0},
    {0, 0, 1, 0, 0},
    {0, 2, 1, 0, 0}
};

public static double[][] board;

public static Rectangle2D setBoard()
{
Rectangle2D.Double tile = new Rectangle2D.Double(10, 10, 10, 10);
for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
    board[i][j] = tile;
    }
}
}



public static int rows = 6;
public static int columns = 5;
public static int[][] next = new int[rows][columns];

public static void main(String[] args)
{
for(int i = 0; i < map.length; i++)
{
    for(int j = 0; j < map[i].length; j++)
    {
    System.out.print(map[i][j] + " ");
    }
    System.out.println();
}

}
}

There is a compile error and it is not letting me store rectangles in this array. Also I have doubts that it would even make a grid.

user1198199
  • 75
  • 3
  • 10

2 Answers2

2

Well, board is an array of double and you are trying to put a Rectangle in there!!! You need:

  public static Rectangle2D[][] board = new Rectangle2D[10][10];

And you need to decide where exactly the Rectangles will be located on screen somehow. You should not create a single Rectangle and place it in each location of the board.

 public static Rectangle2D setBoard()
  {
       Rectangle2D.Double tile;
       for (int i = 0; i < 10; i++)
       {
             for (int j = 0; j < 10; j++)
             {
                 tile = new Rectangle2D.Double(x, y, w, h);//how will you determine x and y here
                 board[i][j] = tile;
             }
       }
 }
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • Then what is the point of storing in a 2D array if I have to determine each x and y coordinate. What would be the best way to draw this board grid??? – user1198199 Mar 31 '12 at 21:20
  • We don't know your point? But if you create a Rectangle with parameters 10, 10, 10, 10 then when you draw them on screen then they will all appear in the same location and look like a single rectangle...so calculate the location of each then store it in your array. – Vincent Ramdhanie Mar 31 '12 at 21:23
  • Couldnt I just skip the storing in a 2d array and have a for loop that creates rectangles with increased x's and y's then drawing them with paintComponent()? – user1198199 Mar 31 '12 at 21:24
  • You could. That would be a simpler solution. – Vincent Ramdhanie Mar 31 '12 at 21:25
2

The code within setBoard has a couple of errors. First, you are creating a single Rectangle2D.Double instance, which you then reuse many times when building the contents of board. This means that if you make a change to any entry in board, all of the entries will be changed - the array contains 100 references to the same object.

The second issue, which is likely causing your compile error, is that board has type double[][] but you're trying to put a Rectangle2D.Double into it, which is a different "double" to your array. You can only put double values into your array.

sgmorrison
  • 948
  • 7
  • 11