4 Replies - 693 Views - Last Post: 19 November 2011 - 07:34 PM Rate Topic: -----

#1 ssjg0ten5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 19-November 11

store grid values in an array

Posted 19 November 2011 - 11:14 AM

Hey guys im new to this website i wan wondering if i could get a bit of help

I am doing a 2d simulation and i have designed a grid, for each cell of the grid I want it to be stored in an array is the below piece of code correct for doing this?
Thank you

//Set grid cells to values in array
    public void setGrid( char [][] data ) 
    {
        for (int i = 0; i < data.length; i++ ) 
        {
            for (int j = 0; j < data[i].length; j++ ) 
            {
                grid.setGrid( i, j, data[i][j] );
            }
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: store grid values in an array

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: store grid values in an array

Posted 19 November 2011 - 12:08 PM

Well it's difficult to know as you say nothing about the app or your data. At the moment, you appear to be storing at least some non-printing characters - why?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: store grid values in an array

Posted 19 November 2011 - 03:11 PM

grid.setGrid( i, j, data[i][j] );

what is the grid object that has a method setGrid(int i, int j, int data)
Was This Post Helpful? 0
  • +
  • -

#4 ssjg0ten5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 19-November 11

Re: store grid values in an array

Posted 19 November 2011 - 06:21 PM

View Postpbl, on 19 November 2011 - 03:11 PM, said:

grid.setGrid( i, j, data[i][j] );

what is the grid object that has a method setGrid(int i, int j, int data)


Hey thanks sorry I took so long
The method is

public void setGrid(int i, int j, char value) 
    {

        grid[i][j].setText( "" + value );
        switch (value) 
        {
            default:
                grid[i][j].setOpaque(false);
                
            case 'l': // represents lion
                grid[i][j].setBackground( Color.RED );//changes the square to red to represent lion
                grid[i][j].setOpaque(true);
                break;
            case 'z': // represents zebra
                grid[i][j].setBackground( Color.MAGENTA );//changes the square to magenta to represent zebra
                grid[i][j].setOpaque(true);
                break;
            case 'g':
                grid[i][j].setBackground( Color.GREEN );//changes the square to green to represent grass
                grid[i][j].setOpaque(true);
                break;
        }
    }


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: store grid values in an array

Posted 19 November 2011 - 07:34 PM

so what is grid ? An array of JLabel I could guess ?
If you do not post the appropriate piece of code, we are the blinds leading the blind

and what is your problem ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1