Reputation: 1913
Hello there,
I am running the following code to make a screenshot of my JPanel.
private void makePanelImage(Component panel, String saveAs) {
Dimension size = panel.getSize();
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
// CROP
// BufferedImage cropped = image.getSubimage(9, 31, 2459, 3467);
BufferedImage cropped = image.getSubimage(1800, 2000, 500, 700);
Graphics2D g2 = cropped.createGraphics();
panel.paint(g2);
saveAs = saveAs + ".png";
try {
ImageIO.write(cropped, "png", new File(saveAs));
System.out.println("Image Saved: " + saveAs);
} catch (Exception e) {
e.printStackTrace();
}
}
The problematic part is:
BufferedImage cropped = image.getSubimage(1800, 2000, 500, 700);
No matter what I set X and Y to - it ALWAYS is taken as ignored. As it would be 0 and 0. And I have no idea why? Has anyone encountered this before? The second part - width and height do have effect on the image, I can make it smaller as much as I want - which is nice. But the top left "beginning" of the raster WONT move.
Is this a bug?
Note: What is funny though, the moment I set it to OFFSET + WIDTH > ORIGINAL I get an error - which is logical but still It wont get "moved"
Am I missing here something please?
Upvotes: 0
Views: 348
Reputation: 2682
You can try this
BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
panel.paint(g2);
BufferedImage cropped = image.getSubimage(1800, 2000, 500, 700);
Upvotes: 1