| BuildGraphDemo.java |
1 /****************************************************************************
2 **
3 ** This file is part of yFiles-2.7.
4 **
5 ** yWorks proprietary/confidential. Use is subject to license terms.
6 **
7 ** Redistribution of this file or of an unauthorized byte-code version
8 ** of this file is strictly forbidden.
9 **
10 ** Copyright (c) 2000-2009 by yWorks GmbH, Vor dem Kreuzberg 28,
11 ** 72070 Tuebingen, Germany. All rights reserved.
12 **
13 ***************************************************************************/
14 package demo.view;
15
16 import java.awt.*;
17 import javax.swing.*;
18
19 import y.base.Node;
20 import y.view.Graph2DView;
21 import y.view.EditMode;
22
23 import y.view.Arrow;
24 import y.view.EdgeRealizer;
25 import y.view.NodeRealizer;
26 import y.view.ImageNodeRealizer;
27 import y.view.NodeLabel;
28 import y.view.EdgeLabel;
29 import y.view.Graph2D;
30
31 /**
32 * <p>
33 * Demonstrates simple usage of {@link Graph2DView}, {@link Graph2D}
34 * and {@link EditMode}.
35 * </p>
36 * <p>
37 * This demo creates an initial graph by adding nodes and edges
38 * to the {@link Graph2D} displayed by the main {@link Graph2DView}
39 * view using API calls. It further shows how some graphical node
40 * and edge properties can be set (see {@link #buildGraph()}).
41 * </p>
42 * <p>
43 * Additionally it is shown how the appearance of the default nodes
44 * and edges can be set (see {@link #configureDefaultRealizers()}).
45 * This applies to new nodes and edges added to the initial graph.
46 * Editing the initial graph is possible due to the {@link EditMode}
47 * added to the view.
48 * </p>
49 */
50 public class BuildGraphDemo extends JPanel
51 {
52 Graph2DView view;
53
54 public BuildGraphDemo()
55 {
56 setLayout(new BorderLayout());
57 view = new Graph2DView();
58 EditMode mode = new EditMode();
59 view.addViewMode(mode);
60 add(view);
61
62 buildGraph();
63 configureDefaultRealizers();
64
65 }
66
67 protected void configureDefaultRealizers()
68 {
69 Graph2D graph = view.getGraph2D();
70
71 //change the looks of the default edge
72 EdgeRealizer er = graph.getDefaultEdgeRealizer();
73 //a standard (target) arrow
74 er.setArrow(Arrow.STANDARD);
75 //set diamond source arrow
76 er.setSourceArrow(Arrow.WHITE_DIAMOND);
77
78 //a label for the edge
79 EdgeLabel elabel = new EdgeLabel("LABEL");
80 er.addLabel(elabel);
81
82 //change the looks (and type) of the default node
83
84 //register an image with ImageNodeRealizer.
85 //must be a path name relative to your java CLASSPATH.
86 ImageNodeRealizer ivr = new ImageNodeRealizer();
87 //set the image
88 ivr.setImageURL(getClass().getResource("resource/yicon.png"));
89 //set node size equals to half of original image size
90 ivr.setToImageSize();
91 ivr.setSize(ivr.getWidth()/2, ivr.getHeight()/2);
92 //set a label text
93 ivr.setLabelText("yFiles");
94
95 //set the label model to be 8-pos (eight available positions around node)
96 ivr.getLabel().setModel(NodeLabel.EIGHT_POS);
97
98 //set the label position (S == South of Node)
99 ivr.getLabel().setPosition(NodeLabel.S);
100
101 //use it as default node realizer
102 graph.setDefaultNodeRealizer(ivr);
103
104 }
105
106 void buildGraph()
107 {
108 Graph2D graph = view.getGraph2D();
109
110 Node v = graph.createNode();
111 //get the "graphics realizer" of v2 from the graph
112 NodeRealizer vr = graph.getRealizer(v);
113 vr.setLocation(60,200);
114 vr.setSize(100,30);
115 vr.setFillColor(DemoDefaults.DEFAULT_NODE_COLOR);
116 vr.setLabelText("Hello");
117
118 //create some edges and new nodes
119 for(int i = 0; i < 5; i++)
120 {
121 Node w = graph.createNode(200, 100 + i*50, 80, 30, "Node " + (i+1));
122 graph.createEdge(v,w);
123 }
124 }
125
126 public void start()
127 {
128 JFrame frame = new JFrame(getClass().getName());
129 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
130 addContentTo(frame.getRootPane());
131 frame.pack();
132 frame.setLocationRelativeTo(null);
133 frame.setVisible(true);
134 }
135
136 public final void addContentTo( final JRootPane rootPane )
137 {
138 rootPane.setContentPane(this);
139 }
140
141 public static void main(String[] args) {
142 EventQueue.invokeLater(new Runnable() {
143 public void run() {
144 (new BuildGraphDemo()).start();
145 }
146 });
147 }
148 }
149
150
151