Advertisement
MrPolywhirl

Tile Image

Mar 26th, 2015
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package image;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.net.URL;
  8.  
  9. import javax.imageio.ImageIO;
  10.  
  11. public class ImageTest {
  12.     public static void main(String[] args) {
  13.         tileImage("mister.png", "mister_tile.png", 0xDDAA33, false, 4, 4, 20, 20, 15, 12);
  14.         tileImage("mister.png", "mister_tile2.png", 0xFF69A6, false, 1, 2, 100);
  15.     }
  16.  
  17.     public static void tileImage(String inputFilename, String outputFilename,
  18.             int bgColor, boolean transparent, int spacing, int border,
  19.             int tileCount) {
  20.         tileImage(inputFilename, outputFilename, bgColor, transparent, spacing,
  21.                 spacing, border, border, tileCount, tileCount);
  22.     }
  23.  
  24.     public static void tileImage(String inputFilename, String outputFilename,
  25.             int bgColor, boolean transparent, int spacingX, int spacingY,
  26.             int borderX, int borderY, int tileCountX, int tileCountY) {
  27.         BufferedImage srcImg = loadImage(inputFilename);
  28.         int imgType = srcImg.getType();
  29.         Color fill = new Color(bgColor, transparent);
  30.         int width = srcImg.getWidth();
  31.         int height = srcImg.getHeight();
  32.         int tileWidth = width / tileCountX;
  33.         int tileHeight = height / tileCountY;
  34.         int calcWidth = width + (spacingX * (tileCountX - 1)) + (borderX * 2);
  35.         int calcHeight = height + (spacingY * (tileCountY - 1)) + (borderY * 2);
  36.         int diffWidth = width - tileWidth * tileCountX;
  37.         int diffHeight = height - tileHeight * tileCountY;
  38.         int newWidth = calcWidth - diffWidth;
  39.         int newHeight = calcHeight - diffHeight;
  40.         BufferedImage outImg = new BufferedImage(newWidth, newHeight, imgType);
  41.         Graphics g = outImg.getGraphics();
  42.  
  43.         g.setColor(fill);
  44.         g.fillRect(0, 0, newWidth, newHeight);
  45.  
  46.         for (int row = 0; row < tileCountY; row++) {
  47.             for (int col = 0; col < tileCountX; col++) {
  48.                 int dX = col * tileWidth;
  49.                 int dY = row * tileHeight;
  50.                 BufferedImage tile = srcImg.getSubimage(dX, dY, tileWidth, tileHeight);
  51.                 int offX = (col * (tileWidth + spacingX)) + borderX;
  52.                 int offY = (row * (tileHeight + spacingY)) + borderY;
  53.                 g.drawImage(tile, offX, offY, null);
  54.             }
  55.         }
  56.  
  57.         writeImage(outImg, "tile", outputFilename);
  58.     }
  59.  
  60.     public static BufferedImage loadImage(String filename) {
  61.         URL url = null;
  62.         try {
  63.             ClassLoader loader = CommonUtil.class.getClassLoader();
  64.             url = loader.getResource("resources/" + filename);
  65.             return ImageIO.read(url);
  66.         } catch (Exception e) {
  67.             System.err.println("Could not load image: " + url);
  68.         }
  69.         return null;
  70.     }
  71.  
  72.     public static void writeImage(BufferedImage img, String directory, String filename) {
  73.         try {
  74.             StringBuffer path = new StringBuffer();
  75.             if (directory != null && !directory.isEmpty()) {
  76.                 path.append(directory).append('\\');
  77.             }
  78.             path.append(filename);
  79.             File outputfile = new File(path.toString());
  80.             // http://stackoverflow.com/a/2833883/1762224
  81.             outputfile.getParentFile().mkdirs();
  82.             ImageIO.write(img, "png", outputfile);
  83.         } catch (Exception e) {
  84.             e.printStackTrace();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement