Changing the Drawing Order of Nodes and Edges

Tips & Tricks

Summary

Drawing nodes on top of all edges and vice versa.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

Normally, the edges of a graph that is displayed in a view (an instance of type Graph2DView) are drawn on top of all nodes.

This behavior can be changed to draw nodes on top of all edges using method DefaultGraph2DRenderer.setDrawEdgesFirst(boolean).

The following sample code demonstrates how to do it.
What the sample code does:

  • Creates a new instance of class DefaultGraph2DRenderer,
  • sets the desired behavior (in this case, to draw edges first, i.e., nodes on top of all edges), and
  • registers the newly created DefaultGraph2DRenderer with the view.

void configureGlobalDrawingSequence(Graph2DView view, boolean drawNodesOnTopOfEdges)
{
  DefaultGraph2DRenderer dgr = new DefaultGraph2DRenderer();
  // Drawing edges first implies that nodes are drawn last, i.e., on top of the edges. 
  dgr.setDrawEdgesFirst(drawNodesOnTopOfEdges);

  // Register the freshly configured DefaultGraph2DRenderer with the given view. 
  view.setGraph2DRenderer(dgr);
}

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > View Implementations
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
drawing - sequence - edge - edges - node - nodes - above - below - front - back