For some time I have been playing around with writing games for Android. The game loads the graphics in form of image sprites, so I needed a way to easily stick several png images into one image. I though there would be tons of free applications available for this purpose, but I didn’t find any, so I decided to create my own sprite generator.
The code is short and simple. Please feel free to use it as you please!
-
-
import javax.imageio.ImageIO;
-
import java.awt.*;
-
import java.awt.image.BufferedImage;
-
import java.io.*;
-
import java.util.*;
-
-
public class SpriteGenerator {
-
-
-
if (args.length != 2)
-
{
-
return;
-
}
-
-
-
-
// Read images
-
ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();
-
-
{
-
if (f.isFile())
-
{
-
fileName.length());
-
-
if (ext.equals("png"))
-
{
-
imageList.add(ImageIO.read(f));
-
}
-
}
-
}
-
-
// Find max width and total height
-
int maxWidth = 0;
-
int totalHeight = 0;
-
-
{
-
totalHeight += image.getHeight();
-
-
if (image.getWidth() > maxWidth)
-
maxWidth = image.getWidth();
-
}
-
-
imageList.size(), totalHeight, maxWidth);
-
-
-
// Create the actual sprite
-
-
int currentY = 0;
-
{
-
g.drawImage(image, 0, currentY, null);
-
currentY += image.getHeight();
-
}
-
-
}
-
}
-
Output from a run:
java -classpath {...classpath hell...} SpriteGenerator ./images ./sprite.png
Number of images: 10, total height: 640px, width: 34px
Writing sprite: /Users/peter/Development/Android/HappyFrog/gfx/sprite.png