BufferedImage tilebmp = null;  tilebmp = ImageIO.read(new URL(imgFilename));  if (tilebmp == null) { throw new Exception("Failed to load " + tilebmpFile); }  int iw = tilebmp.getWidth(); int ih = tilebmp.getHeight();  if (iw > 0 && ih > 0) { for (int y = 0; y <= ih - tileHeight; y += tileHeight + spacing) { for (int x = 0; x <= iw - tileWidth; x += tileWidth + spacing) { BufferedImage tile = new BufferedImage( tileWidth, tileHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D tg = tile.createGraphics();  tg.drawImage(tilebmp, 0, 0, tileWidth, tileHeight, x, y, x + tileWidth, y + tileHeight, null); int newId = addImage(tile); if (createTiles) { Tile newTile = new Tile(); newTile.setImage(newId); addNewTile(newTile); } } } }
importTileBitmap(ImageIO.read(new URL(imgFilename)), tileWidth, tileHeight, spacing, createTiles);
    public void importTileBitmap(String imgFilename, int tileWidth,            int tileHeight, int spacing, boolean createTiles) throws Exception{        File imgFile = null;        try {            imgFile = new File(imgFilename);            tilebmpFile = imgFile.getCanonicalPath();        } catch (IOException e) {            tilebmpFile = imgFilename;        }        System.out.println("Importing " + imgFilename + "...");        BufferedImage tilebmp = null;        tilebmp = ImageIO.read(new URL(imgFilename));        if (tilebmp == null) {            throw new Exception("Failed to load " + tilebmpFile);        }        int iw = tilebmp.getWidth();        int ih = tilebmp.getHeight();        if (iw > 0 && ih > 0) {            for (int y = 0; y <= ih - tileHeight; y += tileHeight + spacing) {                for (int x = 0; x <= iw - tileWidth; x += tileWidth + spacing) {                    BufferedImage tile = new BufferedImage(                            tileWidth, tileHeight,                            BufferedImage.TYPE_INT_ARGB);                    Graphics2D tg = tile.createGraphics();                    tg.drawImage(tilebmp, 0, 0,                            tileWidth, tileHeight,                            x, y, x + tileWidth, y + tileHeight,                            null);                    int newId = addImage(tile);                    if (createTiles) {                        Tile newTile = new Tile();                        newTile.setImage(newId);                        addNewTile(newTile);                    }                }            }        }    }