1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 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 java.awt.event.*;
18  import javax.swing.*;
19  
20  import y.base.Node;
21  import y.base.Edge;
22  
23  import y.view.Graph2DView;
24  import y.view.EditMode;
25  
26  import y.view.Arrow;
27  import y.view.EdgeRealizer;
28  import y.view.NodeRealizer;
29  import y.view.ImageNodeRealizer;
30  import y.view.NodeLabel;
31  import y.view.EdgeLabel;
32  import y.view.Graph2D;
33  
34  /** 
35   *  Demonstrates simple usage of the Graph2DView.
36   *  This demo shows how to add nodes and edges 
37   *  with API call to the view.
38   *  It further shows how some graphical node and edge
39   *  properties can be set.
40   *
41   *  Additionally it is shown how the appearance 
42   *  of the default nodes and edges can be set.
43   *
44   */
45  public class BuildGraphDemo extends JPanel 
46  {
47    Graph2DView view;
48    
49    public BuildGraphDemo()
50    {
51      setLayout(new BorderLayout());  
52      view = new Graph2DView();
53      EditMode mode = new EditMode();
54      view.addViewMode(mode);
55      add(view);
56      
57      buildGraph();
58      setDefaultGraphics();
59      
60    }
61    
62    void setDefaultGraphics()
63    {
64      Graph2D graph = view.getGraph2D();
65      
66      //change the looks of the default edge 
67      EdgeRealizer er = graph.getDefaultEdgeRealizer();
68      //a standard (target) arrow
69      er.setArrow(Arrow.STANDARD); 
70      //set diamond source arrow
71      er.setSourceArrow(Arrow.WHITE_DIAMOND); 
72      
73      //a label for the edge
74      EdgeLabel elabel = new EdgeLabel("LABEL");
75      er.addLabel(elabel);
76      
77      //change the looks (and type) of the default node
78      
79      //register an image with ImageNodeRealizer.
80      //must be a path name relative to your java CLASSPATH.
81      ImageNodeRealizer ivr = new ImageNodeRealizer();
82      //set the image
83      ivr.setImageURL(getClass().getResource("resource/penguin.gif")); 
84      //set node size equals to image size (otherwise scaled)
85      ivr.setToImageSize();
86      //set the label text
87      ivr.setLabelText("Won't bite!"); 
88      
89      //set the label model to be 8-pos (eight available positions aroiund node)
90      ivr.getLabel().setModel(NodeLabel.EIGHT_POS);
91      
92      //set the label position (S == South of Node)
93      ivr.getLabel().setPosition(NodeLabel.S); 
94      
95      //use it as default node realizer
96      graph.setDefaultNodeRealizer(ivr);
97      
98    }
99  
100   void buildGraph()
101   {
102     Graph2D graph = view.getGraph2D();
103     
104     Node v = graph.createNode();
105     //get the "graphics realizer" of v2 from the graph
106     NodeRealizer vr = graph.getRealizer(v);
107     vr.setLocation(60,200);
108     vr.setSize(100,30);
109     vr.setFillColor(Color.cyan);
110     vr.setLabelText("wow! Cyan");
111     
112     //create some edges and new nodes
113     for(int i = 0; i < 5; i++)
114     {
115       Node w = graph.createNode(200, 100 + i*50, "" + (i+1));
116       Edge e = graph.createEdge(v,w);
117     }
118   }
119 
120   public void start()
121   {
122     JFrame frame = new JFrame(getClass().getName());
123     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124     addContentTo(frame.getRootPane());
125     frame.pack();
126     frame.setLocationRelativeTo(null);
127     frame.setVisible(true);
128   }
129 
130   public final void addContentTo( final JRootPane rootPane )
131   {
132     rootPane.setContentPane(this);
133   }
134 
135   public static void main(String args[])
136   {
137     final BuildGraphDemo demo = new BuildGraphDemo();
138     demo.start();
139   }
140 }
141 
142       
143