创建一个不可见的AWT组件的图像? [英] Create an image from a non-visible AWT Component?

查看:154
本文介绍了创建一个不可见的AWT组件的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个不可见AWT组件的图像(屏幕截图)。我不能使用机器人班'屏幕捕获功能,因为该组件不显示在屏幕上。尝试使用下面的code:

I'm trying to create an image (screen-shot) of a non-visible AWT component. I can't use the Robot classes' screen capture functionality because the component is not visible on the screen. Trying to use the following code:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
component.paintAll(g);

有时可行,但是如果组件包含的东西,如一个文本框或按钮,或某种的OpenGL / 3D组件不工作(这些东西都排除在外的形象!)。我怎么可以把整个事情的一个适当的截图?

Works sometimes, but does not work if the component contains things such as a text box or button, or some sort of OpenGL / 3D component (these things are left out of the image!). How can I take a proper screenshot of the whole thing?

推荐答案

非常好的问题,我想过这个自己不时!

Excellent question, I've thought about this myself from time to time!

正如你已经写了,那撕心裂肺的重量重的组件,如3D和AWT到图像是一个大问题。这些组件是(几乎)的直接传送到显卡所以它们不能被重新绘制到使用正常的的paintComponent 的东西的图片,你需要从手术系统的帮助,或做你自己的这些组​​件的渲染。

As you already have written, that rending heavy weight components such as 3D and AWT onto an image is a big problem. These components are (almost) directly transferred to the graphic card so they cannot be re-rendered to an image using the normal paintComponent stuff, you need help from the operative system or doing your own rendering of these components.

有关不具有每个组件图像渲染的方法,你需要创建自己的。例如使用 JOGL 你可以使用这个的方法(的SO~~MD~~aux帖子)。

For each component that does not have a to image rendering method you need to create your own. For example using jogl you can take a off-screen screenshot using this method (SO post).

prerequisites:

Prerequisites:


  1. 您可以启动程序/在无头环境的组成部分?

  2. 您使用Linux呢?

然后你可以使用的Xvfb 渲染整个项目到一个虚拟屏幕,然后采取了从像这样的虚拟屏幕截图:

Then you can use Xvfb to render the whole program onto a virtual screen and then taking a screenshot from that virtual screen like this:

Xvfb :1 &
DISPLAY=:1 java YourMainClass
xwd -display :1 -root -out image.xwd

也许你需要通过传递要呈现给它的程序的大小tweek的Xvfb一点点( 0 -screen 1024x768x24 )。

这篇关于创建一个不可见的AWT组件的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆