Properly Customizing the Visual Behavior of Nodes |
| Applies to: yFiles 2.4, yFiles 2.3, yFiles 2.2, yFiles 2.1, yFiles 2.0 |
Type: Tips & Tricks
Categories this article belongs to:
| yFiles for Java | > yFiles Viewer | > Displaying and Editing Graphs | > Bringing Graph Elements to Life: The Realizer Concept |
Properly customizing the visual behavior of nodes depends on the correct specialization of class NodeRealizer.
In yFiles, visual representation of graph elements is handled by so-called "realizer" classes.
There are several different NodeRealizer
and also several different EdgeRealizer
implementations available.
| Note |
| Since yFiles 2.3 GenericNodeRealizer |
To customize the default visual representation of nodes, abstract class NodeRealizer offers various setter methods.
However, there are cases where sub-classing of NodeRealizer becomes necessary.
Then, it is important to customize all necessary methods a proper realizer needs.
Most importantly, a realizer implementation has to provide its own 'createCopy()' method that supersedes NodeRealizer.createCopy(y.view.NodeRealizer)
.
The following sample code is a demo application that defines a minimalistic specialization of class ShapeNodeRealizer
.
What the sample code does:
- Creates a small demo where the default node type has customized selection indication behavior. In particular, selecting a node does not darken the node's fill color. Instead, only the resize knobs are painted around the node's border.
/* * CustomSelectionColor.java * */ import demo.view.ViewActionDemo; import java.awt.*; import y.base.*; import y.view.*; public class CustomSelectionColor extends ViewActionDemo { Graph2D graph = null; public CustomSelectionColor() { MyShapeNodeRealizer msnr = new MyShapeNodeRealizer(); // The node's original fill color should be used when it gets selected. // Effectively, this means that there are only the resize knobs painted. msnr.useOriginalFillColor(true); graph = view.getGraph2D(); graph.setDefaultNodeRealizer(msnr); } public class MyShapeNodeRealizer extends ShapeNodeRealizer { protected boolean useOriginalFillColor = true; public void useOriginalFillColor(boolean value) { useOriginalFillColor = value; } public boolean isOriginalFillColor() { return useOriginalFillColor; } // Defines customized selection indication behavior. protected void paintNode(Graphics2D gfx) { if (isSelected()) { final Color originalFillColor = getFillColor(); if (useOriginalFillColor) { Color c = new Color(0, 0, 0){ public Color darker(){ return originalFillColor; } }; setFillColor(c); } super.paintNode(gfx); setFillColor(originalFillColor); } else super.paintNode(gfx); } public MyShapeNodeRealizer() { super(); } public MyShapeNodeRealizer(NodeRealizer arg) { super(arg); } // This method is essential. // Without it, class MyShapeNodeRealizer would not work properly! public NodeRealizer createCopy(NodeRealizer arg) { MyShapeNodeRealizer msnr = new MyShapeNodeRealizer(arg); msnr.useOriginalFillColor = this.useOriginalFillColor; return r; } } public static void main(String args[]) { CustomSelectionColor demo = new CustomSelectionColor(); demo.startInFrame("Custom Selection Color"); } } |
| Keywords: | custom - NodeRealizer - extending - subclassing - ShapeNodeRealizer - selection - generic - GenericNodeRealizer |


