Java getSubimage()位于光栅外部

Java getSubimage()位于光栅外部,java,bufferedimage,raster,Java,Bufferedimage,Raster,我正在尝试拍摄一个图像并将其存储在一个16x16子图像的数组中。我使用的图像是512x512像素。但是,在循环中迭代时,getSubimage()会因光栅异常而停止 代码如下: public class TileList extends JPanel { private static final int width = 16; //width of a tile private static final int height = width; private in

我正在尝试拍摄一个图像并将其存储在一个16x16子图像的数组中。我使用的图像是512x512像素。但是,在循环中迭代时,getSubimage()会因光栅异常而停止

代码如下:

public class TileList extends JPanel {

  private static final int width = 16;          //width of a tile
  private static final int height = width;
  private int col = 1;
  private int row = 1;

  private BufferedImage image;
  File tilesetImage = new File("image.png");
  BufferedImage tileset[];

  public void loadAndSplitImage (File loadImage) {
    try{
        image = ImageIO.read(loadImage);
    }catch(Exception error) {
        System.out.println("Error: cannot read tileset image.");
    }// end try/catch
    col = image.getWidth()/width;
    row = image.getHeight()/height;
    tileset = new BufferedImage[col*row];
  }// end loadAndSplitImage

  public TileList() {
    loadAndSplitImage(tilesetImage);
    setLayout(new GridLayout(row,col,1,1));
    setBackground(Color.black);

    int x=0;
    int y=0;
    int q=0;                                    //keeps track of tile #
    for (int i = 0; i < row; i++) {

        for (int j = 0; j < col; j++) {
            JPanel panel = new JPanel();
            tileset[q] = new BufferedImage(width, height, image.getType());
            tileset[q] = image.getSubimage(x,y,x + width,y + height);
            panel.add(new JLabel(new ImageIcon(tileset[q])));
            add(panel);
            x += width;
            q++;
        }// end for loop
        y += height;
        x = 0;
    }// end for loop
  }// end constructor
}// end class
公共类TileList扩展了JPanel{
私有静态final int width=16;//磁贴的宽度
专用静态最终整数高度=宽度;
私有int col=1;
私有int行=1;
私有缓冲图像;
File tilesetImage=新文件(“image.png”);
BuffereImage tileset[];
公共void loadAndSplitImage(文件loadImage){
试一试{
image=ImageIO.read(loadImage);
}捕获(异常错误){
System.out.println(“错误:无法读取tileset图像”);
}//结束尝试/接球
col=image.getWidth()/width;
row=image.getHeight()/height;
tileset=新的缓冲区图像[列*行];
}//结束加载和拆分图像
公共瓷砖商(){
加载和拆分图像(瓷砖估计);
setLayout(新网格布局(行、列、1,1));
挫折背景(颜色:黑色);
int x=0;
int y=0;
int q=0;//跟踪平铺#
对于(int i=0;i
这就是错误:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (x
+ width) is outside of Raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleav
edRaster.java:1245)
    at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1173)
    at TileList.<init>(TileList.java:59)
    at TileList.createAndShowGui(TileList.java:79)
    at TileList$1.run(TileList.java:88)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
线程“AWT-EventQueue-0”java.AWT.image.RasterFormatException中的异常:(x) +宽度)在光栅外 在sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleav edmaster.java:1245) 位于java.awt.image.BuffereImage.getSubimage(BuffereImage.java:1173) 在TileList。(TileList.java:59) 在TileList.createAndShowGui(TileList.java:79) TileList$1.run(TileList.java:88) 在java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)中 位于java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721) 在java.awt.EventQueue.access$200(EventQueue.java:103) 在java.awt.EventQueue$3.run(EventQueue.java:682) 在java.awt.EventQueue$3.run(EventQueue.java:680) 位于java.security.AccessController.doPrivileged(本机方法) 在java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo main.java:76) 位于java.awt.EventQueue.dispatchEvent(EventQueue.java:691) 位于java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:244) 在java.awt.EventDispatchThread.PumpeEventsforFilter(EventDispatchThread。 爪哇:163) 在java.awt.EventDispatchThread.PumpeEventsforHierarchy(EventDispatchThre ad.java:151) 位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 位于java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
您将错误的参数传递给了
getSubimage
。医生说

参数:
x-指定矩形区域左上角的x坐标
y-指定矩形区域左上角的y坐标
w-指定矩形区域的宽度
h-指定矩形区域的高度

您正在传入
x,y,x+width,y+width
,这意味着如果
x
=256,
width
实际上等于
256+16=272

所以你的新形象是
x+width=256+272=528
,比图像区域宽

你应该通过
x,y,width,heigh

tileset[q] = image.getSubimage(x, y, width, height);
来自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
这意味着下面的一行是错误的

image.getSubimage(x,y,x + width,y + height);
应该是这样的

image.getSubimage(x, y, width, height);
有关完整的工作示例,请查看以下内容