We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › 'Graphic' Visualization of an 2D Array
Page Index Toggle Pages: 1
'Graphic' Visualization of an 2D Array (Read 487 times)
'Graphic' Visualization of an 2D Array
Apr 18th, 2008, 5:48am
 
Hello!

I have a 2-dimensional array of 1's and 0's that I'm trying to draw (where each item in the array is a square on the screen) but whenever I try to assign or read from the array I get an OutOfBonds error.

Code:
int inc = 10;
color on = color(204, 102, 0);
color off = color(153);
int[][] boxes = new int[width/10][height/10];

void setup() {
     size(351, 351);
     //println(width/10);   35
     //println(height/10);  35
     drawGrid();
}

void draw() {
     drawGrid();
}

void drawGrid() {  
   for (int x=0; x<boxes.length; x++) {       // cycle 0-35
       for (int y=0; y<boxes.length; y++) {   // cycle 0-35
           if (boxes[x][y] == 1) {            // the cell is active  
               fill(on);                      // set color to on
                 }
           else {
                       fill(off);                     // set color to off
                 }
           
                 println(x + " " + y);              // print array positions
           rect(x*10, y*10, 10, 10);          // draw the rectangle
       }  
   }  
}

void mousePressed() {
     int arrayX = int(mouseX/10);
     int arrayY = int(mouseY/10);
     
     // swap values
     if (boxes[arrayX][arrayY] == 0) {
           boxes[arrayX][arrayY] = 1;
     }
           
     else {
           boxes[arrayX][arrayY] = 0;
     }
}


Edit: The OutOfBonds error happens whenever I click somewhere (even though it should technically work since the "outOfBonds" is on a number that exists (in this case, between 0 and 35))

Also, the Grid is not displayed at all. I'm thinking it has something to do with the *10 in the rect() ?

Any ideas? Thanks!
Re: 'Graphic' Visualization of an 2D Array
Reply #1 - Apr 18th, 2008, 7:49am
 
Looks like you were constructing the boxes before the width and height variable were set by the size(351, 351) command.

So you can either hardcode the array size to 351/10 == 35, or construct that array after you set the size() in setup().

Code:

int inc = 10;
color on = color(204, 102, 0);
color off = color(153);
int[][] boxes;

void setup() {
size(351, 351);
boxes = new int[width/10][height/10];
//println(width/10); 35
//println(height/10); 35
drawGrid();
}

void draw() {
drawGrid();
}



void drawGrid() {

for (int x = 0; x < boxes.length; x++) { // cycle 0-35
for (int y = 0; y < boxes.length; y++) { // cycle 0-35
if (boxes[x][y] == 1) { // the cell is active
fill(on); // set color to on
} else {
fill(off); // set color to off
}

println(x + " " + y); // print array
// positions
rect(x * 10, y * 10, 10, 10); // draw the rectangle
}
}
}

public void mousePressed() {
int arrayX = mouseX / 10;
int arrayY = mouseY / 10;

// swap values
if (boxes[arrayX][arrayY] == 0) {
boxes[arrayX][arrayY] = 1;
}

else {
boxes[arrayX][arrayY] = 0;
}
}


Re: 'Graphic' Visualization of an 2D Array
Reply #2 - Apr 18th, 2008, 5:49pm
 
That fixed the OutOfBonds error, that's a good thing! The boxes / rect() still aren't displayed though!

Thanks for the help, I can probably figure it out from here!
Page Index Toggle Pages: 1