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 に答える