KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > demo > JGraphAdapterDemo


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* ----------------------
26  * JGraphAdapterDemo.java
27  * ----------------------
28  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
29  *
30  * Original Author: Barak Naveh
31  * Contributor(s): -
32  *
33  * $Id: JGraphAdapterDemo.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 03-Aug-2003 : Initial revision (BN);
38  * 07-Nov-2003 : Adaptation to JGraph 3.0 (BN);
39  *
40  */

41 package org.jgrapht.demo;
42
43 import java.awt.*;
44 import java.awt.geom.*;
45
46 import javax.swing.*;
47
48 import org.jgraph.*;
49 import org.jgraph.graph.*;
50
51 import org.jgrapht.*;
52 import org.jgrapht.ext.*;
53 import org.jgrapht.graph.*;
54
55 // resolve ambiguity
56
import org.jgrapht.graph.DefaultEdge;
57
58
59 /**
60  * A demo applet that shows how to use JGraph to visualize JGraphT graphs.
61  *
62  * @author Barak Naveh
63  * @since Aug 3, 2003
64  */

65 public class JGraphAdapterDemo
66     extends JApplet
67 {
68
69     //~ Static fields/initializers --------------------------------------------
70

71     private static final long serialVersionUID = 3256444702936019250L;
72     private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");
73     private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);
74
75     //~ Instance fields -------------------------------------------------------
76

77     //
78
private JGraphModelAdapter jgAdapter;
79
80     //~ Methods ---------------------------------------------------------------
81

82     /**
83      * An alternative starting point for this demo, to also allow running this
84      * applet as an application.
85      *
86      * @param args ignored.
87      */

88     public static void main(String JavaDoc [] args)
89     {
90         JGraphAdapterDemo applet = new JGraphAdapterDemo();
91         applet.init();
92
93         JFrame frame = new JFrame();
94         frame.getContentPane().add(applet);
95         frame.setTitle("JGraphT Adapter to JGraph Demo");
96         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
97         frame.pack();
98         frame.setVisible(true);
99     }
100
101     /**
102      * {@inheritDoc}
103      */

104     public void init()
105     {
106         // create a JGraphT graph
107
ListenableGraph<String JavaDoc, DefaultEdge> g =
108             new ListenableDirectedMultigraph<String JavaDoc, DefaultEdge>(
109                 DefaultEdge.class);
110
111         // create a visualization using JGraph, via an adapter
112
jgAdapter = new JGraphModelAdapter<String JavaDoc, DefaultEdge>(g);
113
114         JGraph jgraph = new JGraph(jgAdapter);
115
116         adjustDisplaySettings(jgraph);
117         getContentPane().add(jgraph);
118         resize(DEFAULT_SIZE);
119
120         String JavaDoc v1 = "v1";
121         String JavaDoc v2 = "v2";
122         String JavaDoc v3 = "v3";
123         String JavaDoc v4 = "v4";
124
125         // add some sample data (graph manipulated via JGraphT)
126
g.addVertex(v1);
127         g.addVertex(v2);
128         g.addVertex(v3);
129         g.addVertex(v4);
130
131         g.addEdge(v1, v2);
132         g.addEdge(v2, v3);
133         g.addEdge(v3, v1);
134         g.addEdge(v4, v3);
135
136         // position vertices nicely within JGraph component
137
positionVertexAt(v1, 130, 40);
138         positionVertexAt(v2, 60, 200);
139         positionVertexAt(v3, 310, 230);
140         positionVertexAt(v4, 380, 70);
141
142         // that's all there is to it!...
143
}
144
145     private void adjustDisplaySettings(JGraph jg)
146     {
147         jg.setPreferredSize(DEFAULT_SIZE);
148
149         Color c = DEFAULT_BG_COLOR;
150         String JavaDoc colorStr = null;
151
152         try {
153             colorStr = getParameter("bgcolor");
154         } catch (Exception JavaDoc e) {
155         }
156
157         if (colorStr != null) {
158             c = Color.decode(colorStr);
159         }
160
161         jg.setBackground(c);
162     }
163
164     @SuppressWarnings JavaDoc("unchecked") // FIXME hb 28-nov-05: See FIXME below
165
private void positionVertexAt(Object JavaDoc vertex, int x, int y)
166     {
167         DefaultGraphCell cell = jgAdapter.getVertexCell(vertex);
168         AttributeMap attr = cell.getAttributes();
169         Rectangle2D bounds = GraphConstants.getBounds(attr);
170
171         Rectangle2D newBounds =
172             new Rectangle2D.Double(
173                 x,
174                 y,
175                 bounds.getWidth(),
176                 bounds.getHeight());
177
178         GraphConstants.setBounds(attr, newBounds);
179
180         // TODO: Clean up generics once JGraph goes generic
181
AttributeMap cellAttr = new AttributeMap();
182         cellAttr.put(cell, attr);
183         jgAdapter.edit(cellAttr, null, null, null);
184     }
185
186     //~ Inner Classes ---------------------------------------------------------
187

188     /**
189      * a listenable directed multigraph that allows loops and parallel edges.
190      */

191     private static class ListenableDirectedMultigraph<V, E>
192         extends DefaultListenableGraph<V, E>
193         implements DirectedGraph<V, E>
194     {
195         private static final long serialVersionUID = 1L;
196
197         ListenableDirectedMultigraph(Class JavaDoc<E> edgeClass)
198         {
199             super(new DirectedMultigraph<V, E>(edgeClass));
200         }
201     }
202 }
203
Popular Tags