Guest User

Untitled

a guest
Jan 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package tool;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import javax.imageio.ImageIO;
  7.  
  8. /**
  9.  *
  10.  * @author Vipar
  11.  */
  12. public class TileSeparator {
  13.     private BufferedImage image;
  14.     private int tileSize;
  15.     private String destinationFolder;
  16.    
  17.     public TileSeparator(BufferedImage image, int tileSize,
  18.             String destinationFolder) {
  19.         this.image = image;
  20.         this.tileSize = tileSize;
  21.         this.destinationFolder = destinationFolder;
  22.     }
  23.    
  24.     public void generateTiles() throws IOException {
  25.         int counter = 0;
  26.         int width = image.getWidth() / tileSize;
  27.         int height = image.getHeight() / tileSize;
  28.         int x = 0;
  29.         int y = 0;
  30.         BufferedImage tile;
  31.         File outputFile;
  32.         for(int i = 0; i < height; i++) {
  33.             for(int j = 0; j < width; j++) {
  34.                 tile = image.getSubimage(x, y, tileSize/2, tileSize/2);
  35.                 x += tileSize;
  36.                 outputFile = new File(destinationFolder + counter + ".png");
  37.                 ImageIO.write(tile,"png",outputFile);
  38.                 counter++;
  39.             }
  40.             y += tileSize;
  41.             x = 0;
  42.         }
  43.     }
  44. }
Add Comment
Please, Sign In to add comment