for (int x = 0, gx = 1; x < tilesPerRow && tileAt < tileset.getMaxTileId(); x++, tileAt++) {
for (int x = 0, gx = 1; x < tilesPerRow && tileAt < tilesetMap.size(); x++, tileAt++) {
    public void paint(Graphics g) {        Rectangle clip = g.getClipBounds();        paintBackground(g);        if (tileset != null) {            // Draw the tiles            int twidth = tileset.getTileWidth() + 1;            int theight = tileset.getTileHeight() + 1;            int tilesPerRow = Math.max(1, (getWidth() - 1) / twidth);            int startY = clip.y / theight;            int endY = (clip.y + clip.height) / theight + 1;            int tileAt = tilesPerRow * startY;                        for (int y = startY, gy = startY * theight; y < endY; y++) {                for (int x = 0, gx = 1; x < tilesPerRow && tileAt < tileset.getMaxTileId(); x++, tileAt++) {                    Tile tile = (Tile) tilesetMap.get(tileAt);                    if (tile != null) {                        tile.drawRaw(g, gx, gy + theight, 1.0);                    }                    gx += twidth;                }                gy += theight;            }        }    }