I'm trying to get a portion of an Image using JavaFX, but the code fails when y = 1.
Here is the code:
public static Image crop(Image src, int col, int row) { PixelReader r = src.getPixelReader(); int sx = col * Grid.SIZE; // start x int sy = row * Grid.SIZE; // start y int ex = sx + Grid.SIZE; // end x int ey = sy + Grid.SIZE; // end y int rx = 0; // x to be written int ry = 0; // y to be written System.out.println(sx + ", " + sy + ", " + ex + ", " + ey); WritableImage out = new WritableImage(Grid.SIZE, Grid.SIZE); PixelWriter w = out.getPixelWriter(); for(int y = sy; y < ey; y++, ry++) { for(int x = sx; x < ex; x++, rx++) { int c = r.getArgb(x, y); w.setArgb(rx, ry, c); System.out.println(rx + ", " + ry + ", " + x + ", " + y); } } return out; } Everything goes well, until the y's in the loop become 1, then this happens:
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: 32, 1 at com.sun.prism.Image$BaseAccessor.getIndex(Unknown Source) at com.sun.prism.Image$BaseAccessor.setArgb(Unknown Source) at com.sun.prism.Image.setArgb(Unknown Source) at javafx.scene.image.WritableImage$2.setArgb(Unknown Source) I have no idea what is wrong. I will give any other information I can.
Grid.SIZEis and whatcolandroware supposed to represent in this method? Typically to crop you would expect a start x and y, a width, and height. Two parameters doesn't seem enough.Grid.SIZEequals 32. It is supposed to represent the width and height of the image to be cropped, and thecolandroware the column and row to start cropping onsx = col * Grid.SIZE?colequals 1, the crop will start atx = 32.