Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.newdawn.slick.tools.packulike;
  
  import java.awt.Graphics;
  import java.io.File;
 
A daft image packer

Author(s):
kevin
 
 public class Pack {
Pack the images provided

Parameters:
files The list of file objects pointing at the images to be packed
width The width of the sheet to be generated
height The height of the sheet to be generated
border The border between sprites
out The file to write out to
Returns:
The generated sprite sheet
Throws:
java.io.IOException Indicates a failure to write out files
 
 	public Sheet pack(ArrayList filesint widthint heightint borderFile outthrows IOException {
 		ArrayList images = new ArrayList();
 		
 		try {
 			for (int i=0;i<files.size();i++) {
 				File file = (Filefiles.get(i);
 				Sprite sprite = new Sprite(file.getName(), ImageIO.read(file));
 				
 				images.add(sprite);
 			}
 		} catch (Exception e) {
 		}
 		
 		return packImages(imageswidthheightborderout);
 	}

Pack the images provided

Parameters:
images The list of sprite objects pointing at the images to be packed
width The width of the sheet to be generated
height The height of the sheet to be generated
border The border between sprites
out The file to write out to
Returns:
The generated sprite sheet
Throws:
java.io.IOException Indicates a failure to write out files
 
 	public Sheet packImages(ArrayList imagesint widthint heightint borderFile outthrows IOException {
 		Collections.sort(imagesnew Comparator() {
 			public int compare(Object o1Object o2) {
 				Sprite a = (Spriteo1;
 				Sprite b = (Spriteo2;
 				
 				int asize = a.getHeight();
 				int bsize = b.getHeight();
 				return bsize - asize;
 			}
 		});
 		
 		int x = 0;
 		int y = 0;
 		
 		BufferedImage result = new BufferedImage(widthheight.);
 		Graphics g = result.getGraphics();
 		int rowHeight = 0;
 		
 		try {
 			PrintStream pout = null;
 			if (out != null) {
 				pout = new PrintStream(new FileOutputStream(new File(out.getParentFile(), out.getName()+".xml")));
 				pout.println("<sheet>");
 			}
 			
 			for (int i=0;i<images.size();i++) {
 				Sprite current = (Spriteimages.get(i);
 				if (x + current.getWidth() > width) {
 					x = 0;
 					y += rowHeight;
 					rowHeight = 0;
 				}
 				
 				if (rowHeight == 0) {
 					rowHeight = current.getHeight() + border;
 				}
 				
 				if (out != null) {
 					pout.print("\t<sprite ");
					pout.print("name=\""+current.getName()+"\" ");
					pout.print("x=\""+x+"\" ");
					pout.print("y=\""+y+"\" ");
					pout.print("width=\""+current.getWidth()+"\" ");
					pout.print("height=\""+current.getHeight()+"\" ");
					pout.println("/>");
				}
				current.setPosition(x,y);
				g.drawImage(current.getImage(), xynull);
				x += current.getWidth() + border;
			}
			if (out != null) {
				pout.println("</sheet>");
				pout.close();
			}
catch (Exception e) {
			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) {
				IOException io = new IOException("Failed writing image");
				io.initCause(e);
				throw io;
			}
		}
		return new Sheet(resultimages);
	}

Entry point to the tool, just pack the current directory of images

Parameters:
argv The arguments to the program
Throws:
java.io.IOException Indicates a failure to write out files
	public static void main(String[] argvthrows 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<files.length;i++) {
			if (files[i].getName().endsWith(".png")) {
				if (!files[i].getName().startsWith("output")) {
					list.add(files[i]);
				}
			}
		}
		Pack packer = new Pack();
		packer.pack(list, 512, 512, 1, new File(dir"output.png"));
		..println("Output Generated.");
	}
New to GrepCode? Check out our FAQ X