2013-01-11 40 views
3

我試圖拍攝一張圖像並將其存儲在16x16子圖像的數組中。我使用的圖像是512x512像素。但是,在遍歷循環時,getSubimage()會被Raster異常停止。柵格之外的Java 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) 

回答

9

你傳遞了​​錯誤的參數getSubimage。該文件說...

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

你傳遞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); 
0

根據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); 

對於一個完整的工作示例,請看paste