[vtkusers] Need help getting vtkCanvas as Image via swing or AWT

Christopher M. Navarro cnavarro at ncsa.uiuc.edu
Tue Apr 22 16:59:57 EDT 2003



On Tue, 22 Apr 2003, Alan wrote:

> Hi,
> I'm trying to capture the image output of the vtkCanvas component.  I've 
> tried
> 
> 
> //    should capture image contents from swing based component
>        RepaintManager 
> rpMgr=RepaintManager.currentManager(panel.getvtkCanvas());
>        img=rpMgr.getOffscreenBuffer(panel, panel.getWidth(), 
> panel.getHeight());
> 
> 
> and also
> 
> 
> //    should capture image contents from awt based component
>        Component c=panel.getvtkCanvas();
>        img=c.createImage(c.getWidth(), c.getHeight());
>        Graphics g=img.getGraphics();
>        c.printAll(g);
> 
> 
> where
> panel extends Jpanel and contains a vtkCanvas() object.
> 
> 
> Although the image size is always correct, the image contents are always 
> empty.  This despite the fact that VTK has displayed a pretty picture 
> onscreen, Java just doesn't seem to be able to access it at either the 
> Swing or AWT level.
> 
> Can anyone help me capture the output of vtkCanvas to an Image()?  Is 
> there a reason that this can't be done?
> 
> Thanks for any insight on this puzzle.
>    --Alan
> 
> 
> BTW, my workaround has been to capture the component via the Robot() class:
> 
>          Robot r=new Robot();
>          Rectangle rect=panel.getBounds();
>          rect.translate(-panel.getInsets().left,-panel.getInsets().top);
>          img=r.createScreenCapture(rect);
> 
> 
> This works acceptably, but only if the vtkCanvas is not covered by any 
> other windows.

If you are using the vtkCanvas supplied with the latest release (which I 
assume you are), you can do the following:

vtkRendererSource renSrc = new vtkRendererSource();
renSrc.SetInput(myVtkCanvas.GetRenderer());
renSrc.WholeWindowOn();

vtkPNGWriter writer = new vtkPNGWriter();
writer.SetInput(renSrc.GetOutput());
writer.SetFileName("test");
writer.Write();

vtkPNGWriter can be replaced by any of the other image writers such as 
vtkJPEGWriter, etc.  

HTH,

chris




More information about the vtkusers mailing list