Skip to content

CaptionWidget

vtk-examples/Java/Widgets/CaptionWidget

Description

vtkCaptionWidget object provides support for interactively placing a caption on the 2D overlay plane.

A caption is defined by some text with a leader (e.g., arrow) that points from the text to a point in the scene.

The caption is represented by a vtkCaptionRepresentation. It uses the event bindings of its superclass (vtkBorderWidget) to control the placement of the text, and adds the ability to move the attachment point around. In addition, when the caption text is selected, the widget emits a ActivateEvent that observers can watch for. This is useful for opening GUI dialogoues to adjust font characteristics, etc.

Other languages

See (Cxx)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

CaptionWidget.java

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

public class CaptionWidget 
{
  // -----------------------------------------------------------------
  // 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 Color = new vtkNamedColors(); 

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

    //Change Color Name to Use your own Color for Renderer Background
    Color.GetColor("red",BgColor);

    //Sphere
    vtkSphereSource sphereSource = new vtkSphereSource();
    sphereSource.Update();

    //Create a mapper and actor
    vtkPolyDataMapper mapper = new vtkPolyDataMapper();
    mapper.SetInputConnection(sphereSource.GetOutputPort());

    vtkActor actor = new vtkActor();
    actor.SetMapper(mapper);

    //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);

    //Create the widget and its representation
    vtkCaptionRepresentation captionRepresentation = new vtkCaptionRepresentation();
    captionRepresentation.GetCaptionActor2D().SetCaption("Test Caption");
    captionRepresentation.GetCaptionActor2D().GetTextActor().GetTextProperty().SetFontSize(100);

    double pos[] = new double[] {0.5, 0, 0};
    captionRepresentation.SetAnchorPosition(pos);

    vtkCaptionWidget captionWidget = new vtkCaptionWidget();
    captionWidget.SetInteractor(iren);
    captionWidget.SetRepresentation(captionRepresentation);

    ren.AddActor(actor);
    ren.SetBackground(BgColor);

    renWin.SetSize(300,300);
    renWin.Render();
    captionWidget.On();

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