java - Cutting shapes from image -
i have buffered image, , array of shapes puzzle pieces. want cut shapes image. tried clipping image doesn't work properly. i've done far is:
(int = 0; < rows; i++) { (int j = 0; j < cols; j++) { area area = areas[i][j]; int width = (int) area.getbounds2d().getwidth(); int height = (int) area.getbounds2d().getheight(); pieces[i][j] = new bufferedimage(width, height, image.gettype()); graphics2d gr = pieces[i][j].creategraphics(); gr.setclip(area); gr.drawimage(image, 0, 0, null); gr.dispose(); try { imageio.write(pieces[i][j], "png", new file("img/img" + + "" + j + ".png")); } catch (ioexception e) { e.printstacktrace(); } } }
it works first shape, writes transparent images output.
actually there plenty of methods this. here approach :)
int width = 0; int height = 0; int rows = 0; int columns = 0; bufferedimage sheet = // image goes here bufferedimage[] pieces = new bufferedimage[rows * cols]; (int = 0; < rows; i++) { (int j = 0; j < cols; j++) { pieces[(i * cols) + j] = sheet.getsubimage(j * width, * height, width, height); } }
note pieces in 1 array. if still want pieces in two-dimensional array:
// switch these lines bufferedimage[][] pieces = new bufferedimage[rows][cols]; pieces[i][j] = sheet.getsubimage(j * width, * height, width, height);
additional reading: spritesheets , animation
click here!
Comments
Post a Comment