有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java只拍摄完整JFrame和JFrame的快照

嘿,伙计们,我已经开发了一个代码来截屏我的整个屏幕,但我希望它只截屏我的Jframe内的东西。顺便说一句,我以后会用它来打印图像的。其中一个主要问题是,鼠标也会进入快照。我不要鼠标或底部的两个按钮。我可以更改按钮的visi,但是对于鼠标和只在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) 个答案

  1. # 1 楼答案

    我认为最好是制作组件的映像(a JFrame也是a 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);