I'm clearly missing something here. I'm trying to cut a slice from an original image, move the remaining larger piece to the left and then append the cut out slice to the right side. Marquee style.
In the attached images, the ORIGINAL image is on top and the resulting BufferedImage from the drawImage functions is on the bottom.
This works:
try {
originalImage = ImageIO.read(new File("c:\\temp\\scanner.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
int sliceSize = 23;
scrolledImage = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = scrolledImage.createGraphics();
leftSlice = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
leftSlice = originalImage.getSubimage(0,0,sliceSize,originalImage.getHeight());
g2.drawImage(leftSlice,originalImage.getWidth() - sliceSize,0,sliceSize,originalImage.getHeight(),null);
rightSlice = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
rightSlice = originalImage.getSubimage(sliceSize,0,originalImage.getWidth() - sliceSize,originalImage.getHeight());
g2.drawImage(rightSlice,0,0,originalImage.getWidth() - sliceSize,originalImage.getHeight(),null);
g2.dispose();
Trying to append to the leftslice with the rightslice does not work:
leftSlice = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
leftSlice = originalImage.getSubimage(0,0,sliceSize,originalImage.getHeight());
Graphics2D left2D = leftSlice.createGraphics();
rightSlice = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
rightSlice = originalImage.getSubimage(sliceSize,0,originalImage.getWidth() - sliceSize,originalImage.getHeight());
left2D.drawImage(rightSlice,0,0,originalImage.getWidth() - sliceSize,originalImage.getHeight(),null);
left2D.dispose();
This is what I get with the second code:
So..it seems to be over-writing the original image. How is that possible when I've implicitly created a new BufferedImage (and createGraphics) and then instructed it to write over new one? Something about the getSubimage I suspect...
I'm just trying to eliminate the need for the extra steps and Buffered images, since this code will be used in many places for small animated elements.
CodePudding user response:
getSubimage() shares the same image does not create a new one - if you read the doc.
So be very very careful with using getSubimage().
This is wasteful (since we are talking about this level of detail):
leftSlice = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
leftSlice = originalImage.getSubimage(0,0,sliceSize,originalImage.getHeight());
creating an image by constructor and then going and overwriting that address with something else. But not a big deal if you don't care about space and in terms of correctness.
getSubimage() will affect your correctness as you have seen..