[vtkusers] Java -how to remove an actor from a vtkpanel?

Stephen mailinglists at icaril.eclipse.co.uk
Wed Aug 15 17:18:41 EDT 2007


Dear all.

Please see the code below. I am trying to add/remove actors to a 
vtkpanel. I find that although the number actors will 
increment/decrement, the view will not refresh unless I bring another 
window in front of it?

I feel I must be doing something silly, I hope someone can point me in 
the right direction, so far I have not found any resources to illustrate 
actor removal in Java.

many thanks

Stephen

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import vtk.*;


public class TestAddRemoveActor extends JPanel implements ActionListener {

     static JFrame frame;
     vtkPanel renWin;
     JButton removeActors;
     JButton addActors;

     vtkActor coneActor = new vtkActor();

     static {
         System.loadLibrary("vtkCommonJava");
         System.loadLibrary("vtkRenderingJava");
     }

   public TestAddRemoveActor() {
     setLayout(new BorderLayout());

       renWin = new vtkPanel();
       vtkConeSource cone = new vtkConeSource();
       cone.SetResolution(60);
       vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
       coneMapper.SetInput(cone.GetOutput());

       coneActor.SetMapper(coneMapper);

       renWin.GetRenderer().AddActor(coneActor);

       removeActors = new JButton("Remove");
       removeActors.addActionListener(this);
    
       addActors    = new JButton("Add actor");
       addActors.addActionListener( this );

       add(renWin, BorderLayout.CENTER);
       add(removeActors, BorderLayout.NORTH);
       add(addActors, BorderLayout.SOUTH);
   }


     public void actionPerformed(ActionEvent e)
     {
         if (e.getSource().equals(addActors)){
             renWin.GetRenderer().Clear();
             vtkConeSource cone = new vtkConeSource();
             cone.SetResolution(60);
             vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
             coneMapper.SetInput(cone.GetOutput());
             coneActor = new vtkActor();
             coneActor.SetMapper(coneMapper);
             renWin.repaint();
             renWin.GetRenderer().AddActor(coneActor);
             System.out.println( 
renWin.GetRenderer().GetActors().GetNumberOfItems() );
        }else{
             renWin.GetRenderer().RemoveActor( coneActor );
             renWin.repaint();                
             System.out.println( 
renWin.GetRenderer().GetActors().GetNumberOfItems() );
         }
     }
    

   public static void main(String s[])
   {
        TestAddRemoveActor panel = new TestAddRemoveActor();

        frame = new JFrame("TestMemoryLeak");
        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        frame.getContentPane().add("Center", panel);
        frame.pack();
        frame.setVisible(true);
    }
}




More information about the vtkusers mailing list