access a cell in a 2d Array grid
in
Programming Questions
•
1 year ago
Hey everybody...
I'm a bit at loss at the moment... I'm not an expert coder so I'm still learning to use basic things such as how to deal with 2d Array...
What I'm trying to do is, I guess, not that hard. I created a grid of objects with the line of code provided on the explanation of a 2dimensional array... like this;
SO this part I get, it's not complicated... What I want to do next somehow gets me confused. What I did is first rendering the grid with a fill(200).
I then wanted to change the color of one of the cell, meaning I have to access it by specifying the indexes of the array, something like this
but it doesn't... any idea why?
I'll write you down also the class file (just in case);
I'm a bit at loss at the moment... I'm not an expert coder so I'm still learning to use basic things such as how to deal with 2d Array...
What I'm trying to do is, I guess, not that hard. I created a grid of objects with the line of code provided on the explanation of a 2dimensional array... like this;
-
Tile [][] grid;//number of rows and columnsint cols;int rows;
void setup() {size(600, 600);cols=width/6;rows=height/6;grid = new Tile[cols][rows]; //the array dimensions are equal to n.of rows/columns
//loop through cols and rowsfor (int i=0; i<cols; i++) {for (int j=0; j<rows; j++) {//initiate objectsgrid[i][j] = new Tile (i*60, j*60, 60, 200);}}}
I then wanted to change the color of one of the cell, meaning I have to access it by specifying the indexes of the array, something like this
- void draw() {
- background(50);
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- //show me them goodies
- grid[i][j].display();
- }
- }
- grid[4][4].black();
- }
but it doesn't... any idea why?
I'll write you down also the class file (just in case);
- class Tile {
- float x, y;
- float side;
- int col;
- Tile (float tempX, float tempY, float tempSide, int tempCol) {
- x = tempX;
- y = tempY;
- side = tempSide;
- col = tempCol;
- }
- void display() {
- rect (x, y, side, side);
- stroke(220);
- fill(col);
- }
- void black() {
- stroke(220);
- fill(0);
- }
- }
1