为createWritableChild切割图像IllegalArgumentException

时间:2011-02-21 18:21:58

标签: java graphics bufferedimage

所以我希望有人能够帮助我解决这个问题。我正在为我的计算机图形学课程编写一个拼图游戏,我在尝试将图像分割成碎片时遇到了一些障碍。我相信问题在于createWritableChild,但是如果这个错误信息并不完全清楚,所以我不确定。这是我的错误:

线程中的异常“AWT-EventQueue-0”java.lang.IllegalArgumentException:Raster ByteInterleavedRaster:width = 45 height = 45 #numDataElements 3 dataOff [0] = 137的minX或minY不等于零:45 0     at java.awt.image.BufferedImage。(Unknown Source)     在JigsawPuzzle.runPuzzleCreate(JigsawPuzzle.java:106)     在JigsawPuzzle.gameBoardCreate(JigsawPuzzle.java:137)     在JigsawPuzzle $ 3.paintComponent(JigsawPuzzle.java:65)

这是我抛出错误的代码:

//Cuts up the given Image and creates the Puzzle and its Pieces.
public Puzzle runPuzzleCreate(BufferedImage p){
    int pieceCount = 150;
    int pieceSize = 45;
    Piece[] pieces = new Piece[pieceCount];
    Piece[] scaled = new Piece[pieceCount];
    Puzzle puzzle = new Puzzle(p, pieces, scaled);
    ColorModel cm = p.getColorModel();
    boolean premult = cm.isAlphaPremultiplied();
    WritableRaster raster = p.copyData(null);
    Posn pos = new Posn(0, 0);
    for(int i = 0; i < pieceCount; i++){
        if(pos.xPos <= 555){
            WritableRaster childRaster = 
                raster.createWritableChild(pos.xPos, pos.yPos,
                        pieceSize, pieceSize,
                        pos.xPos, pos.yPos,
                        null);
            BufferedImage pieceImage = new BufferedImage(cm, childRaster,
                    premult, null);
            Piece piece = new Piece(pieceImage, pos);
            pieces[i] = piece;
            scaled[i] = piece;
            pos.xPos += pieceSize;
        } else if (pos.yPos <= 450){
            pos.yPos += pieceSize;
            pos.xPos = 0;
        } else {
            break;
        }
    }
    return puzzle;
}

1 个答案:

答案 0 :(得分:2)

遗憾的是这个异常没有记录,但是查看它的消息和BufferedImage的源代码,似乎你需要为raster.createWritableChild()方法调用的第五个或第六个参数(或两个)传递0