View Javadoc
1   /*
2    * Copyright (c) 2003, The JUNG Authors
3    * All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    * 
8    */
9   package edu.uci.ics.jung.samples;
10  
11  import java.awt.Color;
12  import java.awt.Container;
13  import java.awt.Dimension;
14  import java.awt.Image;
15  import java.awt.Paint;
16  import java.awt.geom.Point2D;
17  
18  import javax.swing.Icon;
19  import javax.swing.ImageIcon;
20  import javax.swing.JFrame;
21  import javax.swing.JLabel;
22  
23  import com.google.common.base.Functions;
24  
25  import edu.uci.ics.jung.algorithms.layout.KKLayout;
26  import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
27  import edu.uci.ics.jung.graph.util.EdgeType;
28  import edu.uci.ics.jung.visualization.VisualizationImageServer;
29  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
30  import edu.uci.ics.jung.visualization.renderers.GradientVertexRenderer;
31  import edu.uci.ics.jung.visualization.renderers.Renderer;
32  import edu.uci.ics.jung.visualization.renderers.BasicVertexLabelRenderer.InsidePositioner;
33  
34  
35  /**
36   * Demonstrates VisualizationImageServer.
37   * @author Tom Nelson
38   * 
39   */
40  public class VisualizationImageServerDemo {
41  
42      /**
43       * the graph
44       */
45      DirectedSparseMultigraph<String, Number> graph;
46  
47      /**
48       * the visual component and renderer for the graph
49       */
50      VisualizationImageServer<String, Number> vv;
51      
52      /**
53       * 
54       */
55      public VisualizationImageServerDemo() {
56          
57          // create a simple graph for the demo
58          graph = new DirectedSparseMultigraph<String, Number>();
59          String[] v = createVertices(10);
60          createEdges(v);
61  
62          vv =  new VisualizationImageServer<String,Number>(new KKLayout<String,Number>(graph), new Dimension(600,600));
63  
64          vv.getRenderer().setVertexRenderer(
65          		new GradientVertexRenderer<String,Number>(
66          				Color.white, Color.red, 
67          				Color.white, Color.blue,
68          				vv.getPickedVertexState(),
69          				false));
70          vv.getRenderContext().setEdgeDrawPaintTransformer(Functions.<Paint>constant(Color.lightGray));
71          vv.getRenderContext().setArrowFillPaintTransformer(Functions.<Paint>constant(Color.lightGray));
72          vv.getRenderContext().setArrowDrawPaintTransformer(Functions.<Paint>constant(Color.lightGray));
73          
74          vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
75          vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
76          vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);
77  
78          // create a frome to hold the graph
79          final JFrame frame = new JFrame();
80          Container content = frame.getContentPane();
81  
82          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
83          
84          Image im = vv.getImage(new Point2D.Double(300,300), new Dimension(600,600));
85          Icon icon = new ImageIcon(im);
86          JLabel label = new JLabel(icon);
87          content.add(label);
88          frame.pack();
89          frame.setVisible(true);
90      }
91      
92      /**
93       * create some vertices
94       * @param count how many to create
95       * @return the Vertices in an array
96       */
97      private String[] createVertices(int count) {
98          String[] v = new String[count];
99          for (int i = 0; i < count; i++) {
100         	v[i] = "V"+i;
101             graph.addVertex(v[i]);
102         }
103         return v;
104     }
105 
106     /**
107      * create edges for this demo graph
108      * @param v an array of Vertices to connect
109      */
110     void createEdges(String[] v) {
111         graph.addEdge(new Double(Math.random()), v[0], v[1], EdgeType.DIRECTED);
112         graph.addEdge(new Double(Math.random()), v[0], v[3], EdgeType.DIRECTED);
113         graph.addEdge(new Double(Math.random()), v[0], v[4], EdgeType.DIRECTED);
114         graph.addEdge(new Double(Math.random()), v[4], v[5], EdgeType.DIRECTED);
115         graph.addEdge(new Double(Math.random()), v[3], v[5], EdgeType.DIRECTED);
116         graph.addEdge(new Double(Math.random()), v[1], v[2], EdgeType.DIRECTED);
117         graph.addEdge(new Double(Math.random()), v[1], v[4], EdgeType.DIRECTED);
118         graph.addEdge(new Double(Math.random()), v[8], v[2], EdgeType.DIRECTED);
119         graph.addEdge(new Double(Math.random()), v[3], v[8], EdgeType.DIRECTED);
120         graph.addEdge(new Double(Math.random()), v[6], v[7], EdgeType.DIRECTED);
121         graph.addEdge(new Double(Math.random()), v[7], v[5], EdgeType.DIRECTED);
122         graph.addEdge(new Double(Math.random()), v[0], v[9], EdgeType.DIRECTED);
123         graph.addEdge(new Double(Math.random()), v[9], v[8], EdgeType.DIRECTED);
124         graph.addEdge(new Double(Math.random()), v[7], v[6], EdgeType.DIRECTED);
125         graph.addEdge(new Double(Math.random()), v[6], v[5], EdgeType.DIRECTED);
126         graph.addEdge(new Double(Math.random()), v[4], v[2], EdgeType.DIRECTED);
127         graph.addEdge(new Double(Math.random()), v[5], v[4], EdgeType.DIRECTED);
128     }
129 
130     public static void main(String[] args) 
131     {
132         new VisualizationImageServerDemo();
133         
134     }
135 }