0

I have made a small piece of code to split spritesheets into seperate images...

private BufferedImage sheet, dirt, grass, rock, tree, water;

    int width = 64, height = 64;

        public void split() {
            dirt = sheet.getSubimage(0,0,width,height);
            grass = sheet.getSubimage(width,0,width*2,height);
            rock = sheet.getSubimage(width*2,0,width*3,height);
            tree = sheet.getSubimage(0,height,width,height*2);
            water = sheet.getSubimage(width,height,width*2,height*2);
        }

Now, the first two (dirt and grass) go well as expected. However, the issue is with the rock cropping line. For some reason it drops an exception...

" Exception in thread "Thread-0" java.awt.image.RasterFormatException: (x + width) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1245)

"

So apparently the issue is to do with the x value being "out of bounds". But the x value is (width * 2), so 128pix, which is well in bounds of this image (192x128), which I've attached as proof.

I've also changed the code a little to crop with the x value being 1 for rock but I still get the issue, same with using the same dimensions for anothe bufferedImage.

Sorry for anything wrong in this post, it's my first time.

Thanks in advance

The image

Kakarot117
  • 21
  • 2
  • So 128 is in the Java range 0-128? – John3136 Sep 23 '16 at 01:51
  • I've never done this, but I don't think you are thinking of it correctly in terms of height and width. By the way the docs say, x and y are the upper left corner, which you have, but after that I would think normal width and height would be appropriate here. Since you are STARTING at a certain point, you dont have to compensate for the width or height, which is where I think you are going wrong. Try doing, for width and height (not x and y) just `width, height` and the reason `grass` doesn't give you an error is because you have a third option `rock` next to it. – Hypnic Jerk Sep 23 '16 at 02:15

1 Answers1

1

Answering off my comment.

So you are on the right path, but not quite understanding how getSubimage() works.

The docs say

Parameters:

x - the X coordinate of the upper-left corner of the specified rectangular region

y - the Y coordinate of the upper-left corner of the specified rectangular region

w - the width of the specified rectangular region

h - the height of the specified rectangular region

You are correctly setting you x and y values, however you are mistaken in setting you width and height values.

Since you are starting at point (x,y) you do not need to compensate for the width and the height like you are, instead just use them as they are.

So, you code would be

public void split() {
            dirt = sheet.getSubimage(0,0,width,height);
            grass = sheet.getSubimage(width,0,width,height);
            rock = sheet.getSubimage(width*2,0,width,height);
            tree = sheet.getSubimage(0,height,width,height);
            water = sheet.getSubimage(width,height,width,height);
        }
Community
  • 1
  • 1
Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32