Java中getSubimage()方法超出了光栅范围。

8
我试图将一张图片分割成一个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

以下是错误信息:

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)
2个回答

15

您正在向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,height

tileset[q] = image.getSubimage(x, y, width, height);

0

来自Java文档

* @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);

要查看一个完整的工作示例,请点击这里paste


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接