2

我使用以下方法尝试从非轻量级组件中获取 BufferedImage,但我得到一个黑色图像,所以它不起作用,我传递给它的组件是来自 JDIC 的 WebBrowser 对象,它是一个非轻量级组件:

  public static BufferedImage getComponentImage(Component aComponent,Rectangle region) throws IOException
  {
     BufferedImage image= new BufferedImage(aComponent.getWidth(),aComponent.getHeight(),BufferedImage.TYPE_INT_RGB);
     Graphics2D g2d=image.createGraphics();
     SwingUtilities.paintComponent(g2d,aComponent,aComponent.getParent(),region);
     g2d.dispose();
/*
     Graphics g = image.getGraphics();
     aComponent.paint(g);
  // aComponent.paintAll(g);
  // SwingUtilities.paintComponent(g,aComponent,aComponent.getParent(),region);
     g.dispose();
*/
     return image;
  }

我也尝试了评论中的行,它们也不起作用,那么如何从java 中的非轻量级组件中捕获 BufferedImage 呢?

4

1 回答 1

0

SwingUtilities.paintComponent 的文档说“如果组件不是轻量级的,就会发生糟糕的事情:崩溃,异常,绘画问题......”所以你的结果并不意外。

CellRenderPane 似乎没有这样的限制:你可以试试。或者,您可以尝试直接在组件上调用paint(),但这通常会出现问题,除非组件正确嵌入到组件层次结构中。

于 2010-10-05T17:07:10.667 回答