仅拍摄完整JFrame和Jframe的快照

时间:2015-05-19 20:45:51

标签: java swing awt awtrobot

嗨,伙计们,我已经开发了一个代码来拍摄我的整个屏幕的屏幕截图,但我希望它只拍摄我Jframe内部的屏幕截图。我将在以后使用它来打印图像。而其中一个主要问题是,鼠标也进入了快照。我不希望鼠标或底部的两个按钮。我可以改变按钮的视角,但是对于鼠标和Jframe内部应该做什么只拍摄?这是我的代码,它需要整个屏幕的屏幕截图。

                try{
                Thread.sleep(1000);
                Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit class                         returns the default toolkit
                Dimension d = tk.getScreenSize();

//Dimension class object stores width & height of the toolkit screen
// toolkit.getScreenSize() determines the size of the screen

                Rectangle rec = new Rectangle(0, 0, d.width, d.height);  
//Creates a Rectangle with screen dimensions,         

                Robot ro = new Robot(); //to capture the screen image
                BufferedImage img = ro.createScreenCapture(rec);



                File f;
                f = new File("myimage.jpg"); // File class is used to write the above generated buffered image to a file
                ImageIO.write(img, "jpg", f);

            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }

1 个答案:

答案 0 :(得分:1)

恕我直言,最好制作一个组件的图像(JFrame也是Component):

BufferedImage img = new BufferedImage(yourComponent.getWidth(), yourComponent.getHeight(), BufferedImage.TYPE_INT_RGB);
yourComponent.paint(img.getGraphics());
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);