Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package image;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.net.URL;
- import javax.imageio.ImageIO;
- public class ImageTest {
- public static void main(String[] args) {
- tileImage("mister.png", "mister_tile.png", 0xDDAA33, false, 4, 4, 20, 20, 15, 12);
- tileImage("mister.png", "mister_tile2.png", 0xFF69A6, false, 1, 2, 100);
- }
- public static void tileImage(String inputFilename, String outputFilename,
- int bgColor, boolean transparent, int spacing, int border,
- int tileCount) {
- tileImage(inputFilename, outputFilename, bgColor, transparent, spacing,
- spacing, border, border, tileCount, tileCount);
- }
- public static void tileImage(String inputFilename, String outputFilename,
- int bgColor, boolean transparent, int spacingX, int spacingY,
- int borderX, int borderY, int tileCountX, int tileCountY) {
- BufferedImage srcImg = loadImage(inputFilename);
- int imgType = srcImg.getType();
- Color fill = new Color(bgColor, transparent);
- int width = srcImg.getWidth();
- int height = srcImg.getHeight();
- int tileWidth = width / tileCountX;
- int tileHeight = height / tileCountY;
- int calcWidth = width + (spacingX * (tileCountX - 1)) + (borderX * 2);
- int calcHeight = height + (spacingY * (tileCountY - 1)) + (borderY * 2);
- int diffWidth = width - tileWidth * tileCountX;
- int diffHeight = height - tileHeight * tileCountY;
- int newWidth = calcWidth - diffWidth;
- int newHeight = calcHeight - diffHeight;
- BufferedImage outImg = new BufferedImage(newWidth, newHeight, imgType);
- Graphics g = outImg.getGraphics();
- g.setColor(fill);
- g.fillRect(0, 0, newWidth, newHeight);
- for (int row = 0; row < tileCountY; row++) {
- for (int col = 0; col < tileCountX; col++) {
- int dX = col * tileWidth;
- int dY = row * tileHeight;
- BufferedImage tile = srcImg.getSubimage(dX, dY, tileWidth, tileHeight);
- int offX = (col * (tileWidth + spacingX)) + borderX;
- int offY = (row * (tileHeight + spacingY)) + borderY;
- g.drawImage(tile, offX, offY, null);
- }
- }
- writeImage(outImg, "tile", outputFilename);
- }
- public static BufferedImage loadImage(String filename) {
- URL url = null;
- try {
- ClassLoader loader = CommonUtil.class.getClassLoader();
- url = loader.getResource("resources/" + filename);
- return ImageIO.read(url);
- } catch (Exception e) {
- System.err.println("Could not load image: " + url);
- }
- return null;
- }
- public static void writeImage(BufferedImage img, String directory, String filename) {
- try {
- StringBuffer path = new StringBuffer();
- if (directory != null && !directory.isEmpty()) {
- path.append(directory).append('\\');
- }
- path.append(filename);
- File outputfile = new File(path.toString());
- // http://stackoverflow.com/a/2833883/1762224
- outputfile.getParentFile().mkdirs();
- ImageIO.write(img, "png", outputfile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement