Grid not aligned

My head is starting to hurt from trying to figure out why my grid renders as this:

http://billedeupload.dk/images/ZqMWJ.png

I have this code to generate my grid from a two-dimensional array (currently just the upper 3 rows)


        // Get the board tiles
        Field[][] tiles = board.getTiles();
        int distanceX = 0;
        int distanceY = 0;
        int currentRowTiles = 0;
        for (int x=0; x < tiles.length ; x++) {
            for (int y=0; y < tiles[x].length ; y++) {
                
                if (currentRowTiles == 16) {
                    distanceX = 0;
                    distanceY += 50;
                    currentRowTiles = 0;
                }
                                
                // Get current tile
                Field tile = tiles[x][y];
                int fieldX = tile.getX() + distanceX;
                int fieldY = tile.getY() + distanceY;
                
                g.setColor(tile.getColor());
                g.fillRect(fieldX, fieldY, 50, 50);
                distanceX += 50;
                currentRowTiles += 1;
                

                
            }
            
        }

I hope someone can help me out!

I use something along the lines of:


for(int x = 0; x < tiles.length; x++) {
    for(int y = 0; y < tiles[x].length; y++) {
        int xpos = x * 50;
        int ypos = y * 50;
        g.fillRect(xpos, ypos, 50, 50);
    }
}

:slight_smile:

Where 50 is the width/height of the tiles

That worked out, thank you!

Indeed. :slight_smile:

No problem! :slight_smile: Let me know if you’re unsure of how it works its magic :stuck_out_tongue: