View difference between Paste ID: 1BMFZy0M and RVgRG88j
SHOW: | | - or go back to the newest paste.
1
import java.util.LinkedList;
2
import java.awt.image.BufferedImage;
3
import javax.imageio.ImageIO;
4
import java.net.URL;
5
import java.awt.image.RasterFormatException;
6
7
public class SpriteSheet
8
{
9
	//	What we're going to store the images in
10
	private BufferedImage[] images;
11
	
12
	//	Holds the location of our sprites
13
	private URL loc;
14
	
15
	//	The image we'll grab our sprites from
16
	private BufferedImage sprites;
17
	
18
	//	Tile size of each image
19
	private int tileSizeX;
20
	private int tileSizeY;
21
	
22
	//	Tiles in the image
23
	private int xTiles;
24
	private int yTiles;
25
	
26
	SpriteSheet(String sourceLoc, int tX, int tY)
27
	{
28
		try
29
		{
30
			//	Find where our resources are stored
31
			loc = this.getClass().getResource(sourceLoc);
32
			
33
		}catch(Exception e)
34
		{
35
			//	Some error handling
36
			System.out.println(sourceLoc + " is missing.");
37
			e.printStackTrace();
38
		}
39
		try
40
		{
41
			//	Get our resources from the URL
42
			sprites = ImageIO.read(loc);
43
			
44
		}catch(Exception e)
45
		{
46
			//	Some error handling
47
			System.out.println("Image missing or corrupted.");
48
			e.printStackTrace();
49
		}
50
		
51
		//	Amount of tiles in the x and y directions
52
		xTiles = tX;
53
		yTiles = tY;
54
		
55
		//	Array stores the images after they're split up
56
		//	The size is the amount of x tiles and y tiles (area)
57
		images = new BufferedImage[xTiles * yTiles];
58
		
59
		//	Figure out the size of the tiles
60
		tileSizeX = sprites.getWidth()/xTiles;
61
		tileSizeY = sprites.getHeight()/yTiles;
62
		
63
		//	Do the actual splitting of the image into the array
64
		loadSprites();
65
	}
66
	
67
	//	Gets the instance of the spritesheet
68
	public BufferedImage[] getSpritesheet() { return images;}
69
	
70
	private void loadSprites()
71
	{
72
		BufferedImage temp;
73
		
74
		for(int x = 0, y = 0, i = 0; x < xTiles; x++)
75
		{
76
			temp = null;
77
			
78
			System.out.println((sprites.getWidth() - (x  * tileSizeX)) + ", " + (sprites.getHeight() - (y * tileSizeY)));
79
			
80
		   /*
81
		    *
82
			*	WHY DON'T YOU WORK GOD DAMN IT
83
			*
84
			*/
85
			
86
			try
87
			{
88
				temp = sprites.getSubimage(x * tileSizeX, y * tileSizeY, (x * tileSizeX) + tileSizeX, (y * tileSizeY) + tileSizeY);
89
				
90
			}catch(RasterFormatException e)
91
			{
92
				System.out.println("Tile size is wrong, out of bounds.");
93
			}
94
			
95
			//	Add the image to the array
96
			images[i] = temp;
97
			
98
			//	Should stop the loop from getting outside the raster
99
			if( i + 1 == images.length)
100
			{
101
				break;
102
			}
103
			
104
			//	Makes the loop jump to the next row to load new images
105
			if(i == xTiles)
106
			{
107-
				y += tileSizeY;
107+
				y ++;
108
				x = 0;
109
			}
110
			
111
			i++;
112
		}
113
	}
114
}