Tthis code works for me (in the JFrame
):
Container c = getContentPane();
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(im.getGraphics());
ImageIO.write(im, "PNG", new File("shot.png"));
Maybe you have used custom panels. If true, try to add super.paint(g)
at the beginning of the paint
methods of your panels.
EDIT: You have to call saveImage
after display the frame:
public static void main(String args[]) {
...
frame.setSize(400, 300);
sam.saveImage(sam.firstpanel);
}
EDIT 2: This is the saved image (is little because the layout, but is the proof that it should work):
I called the saveImage
as last call in the main
, and used a file in the user dir (new File("Screen.png")
) as nIcE cOw said.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…