Cutting up Image IllegalArgumentException for createWritableChild
So I'm hoping somebody will be able to help me out with this. I am writing a Jigsaw Puzzle game for my Computer Graphics class and I have run into a bit of a snag while trying to cut up my image into pieces. I believe the problem lies with the createWritableChild, but truthfully this error message isnt exactly clear so I am not certain. This is my error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Raster ByteInterleavedRaster: width = 45 height = 45 #numDataElements 3 dataOff[0] = 137 has minX or minY not equal to zero: 45 0 at java.awt.image.BufferedImage.(Unknown Source) at JigsawPuzzle.runPuzzleCreate(JigsawPuzzle.java:106) at JigsawPuzzle.gameBoardCreate(JigsawPuzzle.java:137) at JigsawPuzzle$3.paintComponent(JigsawPuzzle.java:65)
Here is my code that throws the error:
//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;
开发者_StackOverflow社区 }
}
return puzzle;
}
It's a shame this exception is not documented, but looking at its message and the source code of BufferedImage, it seems you need to pass 0 for the 5th or sixth argument (or both) of the raster.createWritableChild()
method call.
精彩评论