Class in a nested array bug

I want create 2D array from class, first of is it possible? it would be an iteration of a cross horizontally and vertically, the shift between the crosses would be shifted diagonally to right down

here is the code

int crosswidth=8;
int crossheight=5;

Cross[][]grid;

void setup(){
  size(800,600);

  grid=new Cross[crosswidth][crossheight];
  for(int i=-width/15; i<width+width/15; i=i+((width/15)*crosswidth)){
    for(int j=-width/15;j<height+width/15; j=j+((width/15)*crossheight)){
      grid[i][j]=new Cross();//HERE IS BUG "ArrayIndexOutOfBoundsException 53"
    }
  }


}

void draw(){
  background(255);
  for(int i=-width/15; i<width+width/15; i=i+((width/15)*crosswidth)){
    for(int j=-width/15;j<height+width/15; j=j+((width/15)*crossheight)){
  grid[i][j].display();
    }
  }
}
class Cross{

  int distanceX,distanceY;
  int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,x11,y11,x12,y12;


  Cross(){   
  }

    void display(){

     x1=width/30;//starting point left middletop corner of the cross
     y1=width/30+width/15;
     x2=x1+width/15;
     y2=y1;
     x3=x2;
     y3=width/30;//topleft corner
     x4=x2+width/15;
     y4=y3;
     x5=x4;
     y5=y1;
     x6=x5+width/15;
     y6=y1;
     x7=x6;
     y7=y1+width/15;
     x8=x4;
     y8=y7;
     x9=x4;
     y9=y7+width/15;
     x10=x3;
     y10=y9;
     x11=x3;
     y11=y7;
     x12=x1;
     y12=y7;

      fill(0);
      noStroke();
      beginShape();
      vertex(x1,y1);
      vertex(x2,y2);
      vertex(x3,y3);
      vertex(x4,y4);
      vertex(x5,y5);
      vertex(x6,y6);
      vertex(x7,y7);
      vertex(x8,y8);
      vertex(x9,y9);
      vertex(x10,y10);
      vertex(x11,y11);
      vertex(x12,y12);
      endShape(CLOSE);
  }//end displayt function

}

thanks n.

Tagged:

Answers

  • For example, line 21, your for-loop variable "i" is going from negative values to positive values and they are not continuous. These numbers are not valid to access an array. Can you see the difference?

    for(int i=0; i<crossWidth; i=i++){
      grid[i][]=.....
    }
    

    Kf

Sign In or Register to comment.