Skip to content

VisualizeGraph

vtk-examples/Java/Graphs/VisualizeGraph

Description

This example shows how to construct a graph and visualize it using vtkMutableUndirectedGraph which is an undirected graph with additional functions for adding vertices and edges You can select edges and vertices with the mouse.

Other languages

See (Cxx), (Python)

Question

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

Code

VisualizeGraph.java

import vtk.vtkNativeLibrary;
import vtk.vtkGraphLayoutView;
import vtk.vtkMutableUndirectedGraph;
import vtk.vtkSimple2DLayoutStrategy;

public class VisualizeGraph 
{

  //-----------------------------------------------------------------
  // 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[]) 
  {

    vtkMutableUndirectedGraph G = new vtkMutableUndirectedGraph();
    int v1 = G.AddVertex();
    int v2 = G.AddVertex();

    G.AddGraphEdge(v1, v2);
    G.AddGraphEdge(v1, v2);

    vtkGraphLayoutView GraphLayoutView = new vtkGraphLayoutView();
    GraphLayoutView.AddRepresentationFromInput(G);
    GraphLayoutView.SetLayoutStrategy("Simple 2D");
    GraphLayoutView.ResetCamera();
    GraphLayoutView.Render();


    vtkSimple2DLayoutStrategy Simple2DLayoutStrategy = new vtkSimple2DLayoutStrategy();
    Simple2DLayoutStrategy.SetRandomSeed(0);
    GraphLayoutView.GetLayoutStrategy();
    GraphLayoutView.GetInteractor().Start();

  }

}