Skip to content


# MoveCamera

VTKEx/Java/Visualization/MoveCamera

### Description

This example creates two spheres. They will move together when the mouse is used to interact with the scene because the camera is actually what is moving causing the view of the spheres to change.

Other languages

See (Cxx)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

MoveCamera.java

    import vtk.vtkActor;
    import vtk.vtkInteractorStyleTrackballCamera;
    import vtk.vtkNamedColors;
    import vtk.vtkNativeLibrary;
    import vtk.vtkPolyDataMapper;
    import vtk.vtkRenderWindow;
    import vtk.vtkRenderWindowInteractor;
    import vtk.vtkRenderer;
    import vtk.vtkSphereSource;

    public class MoveCamera
    {
      // -----------------------------------------------------------------
      // Load VTK library and print which library was not properly loaded
      static
      {
        if (!vtkNativeLibrary.LoadAllNativeLibraries())
        {
          for (vtkNativeLibrary lib : vtkNativeLibrary.values())
          {
            if (!lib.IsLoaded())
            {
              System.out.println(lib.GetLibraryName() + " not loaded");
            }
          }
        }
        vtkNativeLibrary.DisableOutputWindow(null);
      }
      // -----------------------------------------------------------------

      public static void main(String args[])
      {
        vtkNamedColors colors = new vtkNamedColors();

        //Renderer Background Color
        double Bgcolor[] = new double[4];

        colors.GetColor("White", Bgcolor);

        //Sphere1
        vtkSphereSource sphereSource1 = new vtkSphereSource();
        sphereSource1.SetCenter(0.0, 0.0, 0.0 );
        sphereSource1.SetRadius(4.0);
        sphereSource1.Update();

        vtkPolyDataMapper sphereMapper1  = new vtkPolyDataMapper();
        sphereMapper1 .SetInputData (sphereSource1.GetOutput());

        vtkActor sphereActor1 = new vtkActor();
        sphereActor1.SetMapper(sphereMapper1);

        //Sphere2
        vtkSphereSource sphereSource2 = new vtkSphereSource();
        sphereSource2.SetCenter(10.0, 0.0, 0.0 );
        sphereSource2.SetRadius(3.0);
        sphereSource2.Update();

        vtkPolyDataMapper sphereMapper2  = new vtkPolyDataMapper();
        sphereMapper2 .SetInputData (sphereSource2.GetOutput());

        vtkActor sphereActor2 = new vtkActor();
        sphereActor2.SetMapper(sphereMapper2);

        // Create the renderer, render window and interactor.
        vtkRenderer ren = new vtkRenderer();
        vtkRenderWindow renWin = new vtkRenderWindow();
        renWin.AddRenderer(ren);
        vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
        iren.SetRenderWindow(renWin);

        // Visualise
        ren.AddActor(sphereActor1);
        ren.AddActor(sphereActor2);

        ren.SetBackground(Bgcolor);

        renWin.SetSize(640, 480);
        renWin.Render();

        vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
        iren.SetInteractorStyle(style);

        iren.Initialize();
        iren.Start();
      }
    }