The way graph elements are created in the SimpleGraphViewer2 application lacks any explicit specification regarding their visual representation. Thus, Graph2D assigns defaults which it is maintaining for both nodes and edges. We will now change these defaults, so that we get an arrowhead decoration at the target end of edges, and also some other default rendering for the nodes.
The SimpleGraphViewer3 application will contain the new default settings.
Let's add new method configureDefaultRealizers, shown in Example 1.5, “Setting new default presentations for graph elements”, which incorporates our changes.
Example 1.5. Setting new default presentations for graph elements
protected void configureDefaultRealizers(Graph2D graph) { // Add an arrowhead decoration to the target side of the edges. graph.getDefaultEdgeRealizer().setTargetArrow(Arrow.STANDARD); // Set the node size and some other graphical properties. NodeRealizer defaultNodeRealizer = graph.getDefaultNodeRealizer(); defaultNodeRealizer.setSize(80, 30); defaultNodeRealizer.setFillColor(Color.ORANGE); defaultNodeRealizer.setLineType(LineType.DASHED_1); }
In this method, we use Graph2D's getter methods to get the default edge realizer and default node realizer, and set new values via the realizer's setter methods. The new defaults will affect all graph elements that are created thereafter, which is why we will add the call to the method to the constructor.
Example 1.6. SimpleGraphViewer3 constructor
public SimpleGraphViewer3(Dimension size, String title) { view = createGraph2DView(); graph = view.getGraph2D(); frame = createApplicationFrame(size, title, view); configureDefaultRealizers(graph); }
Figure 1.4, “New default visual representation for graph elements” shows the results of our changes. The nodes have a new visual appearence and the edge finally indicates its actual direction to the user.
Now that we have the geometry of our nodes defined in the default realizer settings, we no longer need the createNode methods to specify it. We will change the corresponding code in the populateGraph method as follows.
Example 1.7. Updated node creation calls
Node hello = graph.createNode(100, 50, "Hello"); Node world = graph.createNode(100, 100, "World!");
You will find related information in the yFiles for Java Developer's Guide:
In the yFiles for Java API:
In the yFiles for Java source code demos:
Copyright ©2008-2009, yWorks GmbH. All rights reserved. |