views:

26

answers:

1

I used the following method try to get a BufferedImage from a non light weight Component, but I got a black image, so it didn't work, the component I passed to it is a WebBrowser object from JDIC, it's a non light weight Component :

  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;
  }

I've also tried the lines in comments, they didn't work either, so how to capture a BufferedImage from a non light weight Component in java ?

A: 

The documentation of SwingUtilities.paintComponent says "If the Component is not lightweight, bad things map happen: crashes, exceptions, painting problems... " so your results are not unexpected.

CellRenderPane appears to have no such restrictions: you could try that. Alternatively you could try calling paint() on the component directly, but that usually gives problems unless the component is correctly embedded in a component hierarchy.

DJClayworth
Thanks, but in my case, how to use CellRenderPane ? Any sample code ?
Frank