简体   繁体   English

如何使用setVisible(false)从JFrame创建图像?

[英]How to create an image from a JFrame with setVisible(false)?

I created a JFrame which basically creates a grid with some calculation, and I needed to get an image from that and I am currently using: 我创建了一个JFrame,该框架基本上会创建一个经过一定计算的网格,并且我需要从中获取图像,并且当前正在使用:

public static BufferedImage createImageFromFrame(Component component) {
    int w = component.getWidth();
    int h = component.getHeight();
    BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
    component.paint( image.getGraphics() );
    return image;
}

calling like: 像这样打电话

JFrame window = new JFrame();
// set stuff
BufferedImage img = d.createImageFromFrame(window.getContentPane());

And that do just fine. 这样就可以了。 But now I want to create multiples images from different Frames, and I don't want to each one appear on the screen, I just want the images generated from them. 但是现在我想从不同的框架创建多个图像,并且我不想每个图像都出现在屏幕上,我只想从它们生成图像。 But I noticed I cannot use setVisible(false) cause it gives a java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0 . 但是我注意到我不能使用setVisible(false)因为它给出了java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0

Is there any work around, or something to change that may help me accomplished that? 有没有解决的办法,或者有什么改变可以帮助我实现这一目标?

Pack the frame and move it outside of the visible area of your display. 包装框架并将其移到显示屏的可见区域之外。 For example: 例如:

frame.pack();
frame.setLocation(-2000, -2000);
frame.setVisible(true);
// There you go, the frame won't be visible

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM