Java getSubimage() outside of raster

前端 未结 2 1253
陌清茗
陌清茗 2020-12-11 18:41

I\'m trying to take an image and store it in an array of 16x16 subimages. The image I am using is 512x512 pixels. However, while iterating through the loop, getSubimage() is

相关标签:
2条回答
  • 2020-12-11 19:22

    From the javadoc

    * @param x the X coordinate of the upper-left corner of the
    *          specified rectangular region
    * @param y the Y coordinate of the upper-left corner of the
    *          specified rectangular region
    * @param w the width of the specified rectangular region
    * @param h the height of the specified rectangular region
    

    this means the following line is wrong

    image.getSubimage(x,y,x + width,y + height);
    

    it should be something like

    image.getSubimage(x, y, width, height);
    

    For a full working example take a look at this paste

    0 讨论(0)
  • 2020-12-11 19:23

    You're passing the wrong parameters to getSubimage. 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're passing in x, y, x + width, y + width, which would mean if x = 256, width actually equals 256 + 16 = 272.

    So you new image would be ... x + width = 256 + 272 = 528, which is wider then your image area.

    You should be passing x, y, width, heigh

    tileset[q] = image.getSubimage(x, y, width, height);
    
    0 讨论(0)
提交回复
热议问题