GridLayout coordinates

1.9k Views Asked by At

So I think you will understand my problem by this piece of code:

int s = 4;
int v = 4;    

world.setLayout(new GridLayout(s, v));

        grid = new JLabel[s][v];

        for (int x = s-1; x >= 0; x--) {

            for (int y = 0; y < v; y++) {

                grid[x][y] = new JLabel((x)+","+(y));

                world.add(grid[x][y]);

Now I get a grid with coordinates:

3,0  3,1  3,2  3,3
2,0  2,1  2,2  2,3
1,0  1,1  1,2  1,3
0,0  0,1  0,2  0,3

But I would like to get:

0,3  1,3  2,3  3,3
0,2  1,2  2,2  3,2
0,1  1,1  2,1  3,1
0,0  1,0  2,0  3,0

Any help appreciated..

2

There are 2 best solutions below

0
On BEST ANSWER

I didn't test it but try this out:

    for (int y = s-1; y >= 0; y--) {

        for (int x = 0; x < v; x++) {

            grid[x][y] = new JLabel((x)+","+(y));

            world.add(grid[x][y]);
0
On

I did not test it, but try change your code:

grid[x][y] = new JLabel((x)+","+(y));

to:

grid[x][y] = new JLabel((y)+","+(x));