All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.newdawn.slick.tools.packulike.Pack Maven / Gradle / Ivy

The newest version!
package org.newdawn.slick.tools.packulike;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.imageio.ImageIO;

/**
 * A daft image packer
 * 
 * @author kevin
 */
public class Pack {
	/**
	 * Pack the images provided
	 * 
	 * @param files The list of file objects pointing at the images to be packed
	 * @param width The width of the sheet to be generated 
	 * @param height The height of the sheet to be generated
	 * @param border The border between sprites
	 * @param out The file to write out to
	 * @return The generated sprite sheet
	 * @throws IOException Indicates a failure to write out files
	 */
	public Sheet pack(ArrayList files, int width, int height, int border, File out) throws IOException {
		ArrayList images = new ArrayList();
		
		try {
			for (int i=0;i");
			}
			
			for (int i=0;i width) {
					x = 0;
					y += rowHeight;
					rowHeight = 0;
				}
				
				if (rowHeight == 0) {
					rowHeight = current.getHeight() + border;
				}
				
				if (out != null) {
					pout.print("\t");
				}
				
				current.setPosition(x,y);
				g.drawImage(current.getImage(), x, y, null);
				x += current.getWidth() + border;
			}
			g.dispose();
			
			if (out != null) {
				pout.println("");
				pout.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			IOException io = new IOException("Failed writing image XML");
			io.initCause(e);
			
			throw io;
		}
		
		if (out != null) {
			try {
				ImageIO.write(result, "PNG", out);
			} catch (IOException e) {
				e.printStackTrace();
				
				IOException io = new IOException("Failed writing image");
				io.initCause(e);
				
				throw io;
			}
		}
		
		return new Sheet(result, images);
	}
	
	/**
	 * Entry point to the tool, just pack the current directory of images
	 * 
	 * @param argv The arguments to the program
	 * @throws IOException Indicates a failure to write out files
	 */
	public static void main(String[] argv) throws IOException {
		File dir = new File(".");
		dir = new File("C:\\eclipse\\grobot-workspace\\anon\\res\\tiles\\indoor1");
		
		ArrayList list = new ArrayList();
		File[] files = dir.listFiles();
		for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy