code: public static Collection<SpriteTiled> extract(Collection<ImageBuffer> tiles, int horizontalTiles)
      {
          final Surface surface = getSheetSize(tiles, horizontalTiles);
          final int horizontals = surface.getWidth();
          final int verticals = surface.getHeight();
          final int tilesPerSheet = Math.min(tiles.size(), horizontals * verticals);
          final Collection<SpriteTiled> sheets = new ArrayList<>();
  
          ImageBuffer sheet = null;
          Graphic g = null;
          int number = 0;
          int tw = 0;
          int th = 0;
  
          for (final ImageBuffer tile : tiles)
          {
              if (g == null)
              {
                  tw = tile.getWidth();
                  th = tile.getHeight();
  
                  final int width = Math.max(tw, horizontals * tw);
                  final int height = Math.max(th, verticals * th);
                  sheet = Graphics.createImageBuffer(width, height, tile.getTransparentColor());
                  g = sheet.createGraphic();
              }
              final int x = number % horizontals;
              final int y = (int) Math.floor(number / (double) horizontals);
              g.drawImage(tile, x * tw, y * th);
              number++;
  
              if (number >= tilesPerSheet)
              {
                  g.dispose();
                  sheets.add(Drawable.loadSpriteTiled(Graphics.getImageBuffer(sheet), tw, th));
                  sheet = null;
                  g = null;
                  number = 0;
              }
          }
  
          return sheets;
      }
docs: Convert a set of tile to a set of tile sheets.
  
  @param tiles The list of tiles.
  @param horizontalTiles The number of horizontal tiles on sheet (inferior or equal to 0 to use automatic).
  @return The list of tile sheets.