-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteSheet.java
71 lines (62 loc) · 1.78 KB
/
SpriteSheet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.File;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class SpriteSheet {
public static void main(String[] args){
if (args.length!=5){
System.out.println("Please run with 5 command line arguments filepath of input folder, name of sprite sheet to create, length of images in file, width, num images in directory");
System.exit(0);
}
String folderPath = args[0];
String outputFile = args[1];
int width = Integer.parseInt(args[2]);
int height = Integer.parseInt(args[3]);
int numImages = Integer.parseInt(args[4]);
BufferedImage outImage = new BufferedImage(numImages*width, height, BufferedImage.TYPE_INT_ARGB);
int widthStart = 0;
File dir = new File(folderPath);
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
if(!child.isHidden()){
writeImage(readImage(child), outImage, widthStart, width, height);
widthStart+=width;
}
}
//write to file
File out = new File(outputFile + ".png");
try{
ImageIO.write(outImage, "png", out);
}
catch(Exception e){
e.printStackTrace();
}
} else {
System.out.println("No directory found with name " + args[0]);
}
}
public static BufferedImage readImage(File file){
BufferedImage img = null;
try
{
System.out.println(file.getName());
img = ImageIO.read(file);
}
catch (Exception e)
{
e.printStackTrace();
}
return img;
}
public static void writeImage(BufferedImage toWrite, BufferedImage out, int start, int width, int height){
int toWriteCurr = 0;
for(int i=start;i<start+width;i++){
for(int j = 0;j<height;j++){
int rgb = toWrite.getRGB(toWriteCurr, j);
out.setRGB(i, j, rgb);
}
toWriteCurr++;
}
}
}