# DrawText

VTKExamples/Cxx/Visualization/DrawText

### Description

This example writes "Hello world" in the bottom left corner of the render window. The text size and color are set. There are many properties that can be set that control how the text is changed when the render window is resized, but they are not covered in this example.

This example draws text in 2D. For an example using 3D text that can be placed on the scene like a regular vtkActor, see VectorText. For an example of drawing text, also in 2D, but into a vtkImageData object rather than into the render window, see ImageText/ImageText]]([../VectorText]].).

Seealso

TextWidget

### Code

DrawText.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkPolyData.h>
    #include <vtkSphereSource.h>
    #include <vtkTextActor.h>
    #include <vtkTextProperty.h>

    int main(int, char *[])
    {
      // Create a sphere
      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->SetCenter ( 0.0, 0.0, 0.0 );
      sphereSource->SetRadius ( 5.0 );
      sphereSource->Update();

      // Create a mapper
      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputData ( sphereSource->GetOutput() );

      // Create an actor
      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper ( mapper );

      // Create a renderer
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->SetBackground ( 1, 1, 1 ); // Set background color to white
      renderer->AddActor ( actor );

      // Create a render window
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer ( renderer );

      // Create an interactor
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow ( renderWindow );

      // Setup the text and add it to the renderer
      vtkSmartPointer<vtkTextActor> textActor =
        vtkSmartPointer<vtkTextActor>::New();
      textActor->SetInput ( "Hello world" );
      textActor->SetPosition2 ( 10, 40 );
      textActor->GetTextProperty()->SetFontSize ( 24 );
      textActor->GetTextProperty()->SetColor ( 1.0, 0.0, 0.0 );
      renderer->AddActor2D ( textActor );

      // Render and interact
      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(DrawText)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersSources
      vtkInteractionStyle
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping DrawText: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(DrawText MACOSX_BUNDLE DrawText.cxx )
      target_link_libraries(DrawText PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(DrawText MACOSX_BUNDLE DrawText.cxx )
      target_link_libraries(DrawText PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS DrawText
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build DrawText

Click here to download DrawText and its CMakeLists.txt file. Once the tarball DrawText.tar has been downloaded and extracted,

    cd DrawText/build

If VTK is installed:

    cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

    cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

    make

and run it:

    ./DrawText

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.