Java cropping on x and y
OK. so let's say I have this picture: http://i.stack.imgur.com/oYhJy.png
I'm trying to do a crop (which works -- I just have the wrong numbers) of the image into separate image arrays. The tile image (linked above) is 36 tiles wide and 15 tiles long. So that's 1152 pixels in width (32 tile width * 36 tiles) and 480 pixels in height (32 tile height * 15 tiles).
Here's what I have so far:
for (int xi = 0; xi < 522; xi++) {
int cropHeight = 32;
int cropWidth = 32;
int cropStartX = xi*32;
int cropStartY = 0;
if (xi % 36 == 0) {
cropStartY = xi*32;
}
BufferedImage processedImage = cropMyImage(or开发者_运维问答iginalImage, cropWidth, cropHeight, cropStartX, cropStartY);
tiles[xi] = processedImage;
}
What am I doing wrong? It's working technically, but it's getting the wrong tile images.
Probably clearer if you did a double loop rather than trying to use modulus.
int i = 0;
// no need to have these values inside a loop. They are constants.
int cropHeight = 32;
int cropWidth = 32;
for (int x = 0; x < 36; x++) {
for (int y = 0; y < 15; y++) {
int cropStartX = x*32;
int cropStartY = y*32;
BufferedImage processedImage = cropMyImage(originalImage, cropWidth, cropHeight, cropStartX, cropStartY);
tiles[i++] = processedImage;
}
}
Probably should be:
int cropStartX = (xi%36)*32;
int cropStartY = xi/36*32;
精彩评论