获得一个Java小程序的截图 [英] Getting a screenshot of a Java applet

查看:160
本文介绍了获得一个Java小程序的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Java Robot类做各种项目的一些自动化的测试我的工作,我有一些麻烦的任何程序,是不是满屏幕的屏幕截图。

I'm trying to use the Java Robot class to do some automated testing for various projects I've worked on and I'm having some trouble getting screen shots of any program that isn't full screen.

有关全屏程序的话就用:

For full screen programs I just use:

DIM维= Toolkit.getDefaultToolkit()getScreenSize()。
  BufferedImage的图像= robot.createScreenCapture(DIM);

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); BufferedImage image = robot.createScreenCapture (dim);

我知道,我不能让一般的特定窗口的截图,因为我是pretty确保Java不知道屏幕上的每个窗口(因为它的操作系统特定的)。

I know that I can't get a screenshot of a particular window in general, since I'm pretty sure Java doesn't know where on the screen each window is (since its OS specific).

但我希望我能仍然获得在applet的浏览器的applet的screeenshot,因为窗口已连接到JVM以这种或那种方式。

But I'm hoping I could still get a screeenshot of an applet in an applet-viewer, since the window is connected to the JVM in one way or another.

所以,对于这个是否是可能的任何想法?如果是这样,我怎么会去这样做?

So, any ideas on whether or not this is possible? And if so, how I might go about doing it?

推荐答案

假设你有你的小应用程序(或任何其他组件)的引用,您可以创建一个离屏Graphics2D的实例,有组分涂料本身的。

Assuming you have a reference to your applet (or any other Component), you create an off-screen Graphics2D instance and have the component paint itself to that.

  Component applet = ...;    // the applet

  Dimension size = applet.getSize();
  BufferedImage offScreenImage = (BufferedImage) applet.createImage(size.width, size.height);
  Graphics2D g2 = offScreenImg.createGraphics();
  g2.setBackground(applet.getBackground());

  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

  g2.clearRect(0, 0, size.width, size.height);

  applet.paint(g2);

  // now you can use BufferedImage as before

关键是 Component.createImage 这创造了双缓冲的,屏幕图像。

The key is Component.createImage which creates an off-screen image for double buffering.

这篇关于获得一个Java小程序的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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