Split BufferedImage into Array
hi guys, i tried to split a buffered image into an Array.. but it fails
Code:
private BufferedImage imageTileset;
private BufferedImage[] images;
imageTileset = ImageToBufferedImage("http://www.java-forums.org/images/0");
images = splitImage(imageTileset, 16, 16);
public BufferedImage[] splitImage(BufferedImage img, int cols, int rows)
{
int w = img.getWidth()/cols;
int h = img.getHeight()/rows;
int num = 0;
BufferedImage imgs[] = new BufferedImage[w*h];
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < cols; x++)
{
imgs[num] = new BufferedImage(w, h, img.getType()); // => ArrayIndex 4 doesnt work;
// Tell the graphics to draw only one block of the image
Graphics2D g = imgs[num].createGraphics();
g.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
g.dispose();
num++;
}
}
return imgs;
}
public BufferedImage ImageToBufferedImage(String imagepath)
{
BufferedImage img = toBufferedImage(new ImageIcon(imagepath + ".png").getImage());
return img;
}
public BufferedImage toBufferedImage(Image image) {
image = new ImageIcon(image).getImage();
BufferedImage bimage = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
at the line which i wrote the mistake in, it says:
ArrayIndex Bounds out of exception: 4....
i dont know why.. i got the code from the internet and im not that into java , that i can find it :/
help me please
if you need to know: my image is 1024x1024 and a .png...