0

I have an square grid from a GridLayout(4,4,0,0) for a JPanel, just say 4x4 for a small example

*-*-*-*
*-*-*-*
*-*-*-*
*-*-*-*

Say I want to loop through just the top left corner. For a case,

*-*
*

And for another case, the bottom right corner.

  *
*-*

I was thinking of a dynamic loop? For larger size boards.

Bao Thai
  • 533
  • 1
  • 6
  • 25
  • A basic scheme to which you can add offsets is shown [here](http://stackoverflow.com/a/22333914/230513). – trashgod Nov 27 '16 at 21:29
  • I figured it out. I'll post it in answer in case anyone is interested. A dynamic for loop. Could be improved upon. – Bao Thai Nov 27 '16 at 22:59

1 Answers1

0

Case: 1 (Top Left Corner Starting from, say (0,0))

int csize = (number of cols)/2+1;
for (int row = 0; row < (number of cols)/2; row++){
    csize--;
    for (int col = 0; col < csize; col++){
                ...stuff.....
    }
}

Case: 2 (Bottom Right Corner, Starting from say, (4,4))

int csize = (number of cols)/2;
for (int row = (number of cols) -1; row > (number of cols)/2; row--){
    csize++;
    for (int col = (number of cols) -1; col > csize; col--){
                        ...stuff...
    }
}

I kind of just did a initial size of cols, and decrease the nested loop for the column inside of rows

Bao Thai
  • 533
  • 1
  • 6
  • 25