[jgrapht-users] DirectedGraph and JAdapter
Brought to you by:
barak_naveh,
perfecthash
From: <jl...@ol...> - 2011-03-14 17:32:51
|
I am a new JGraph user and I am having trouble drawing my JGraph. I have simply tried editing the JGraphAdapterDemo to use a DirectedGraph instead of ListenableDirected Graph...but it won't work. What am I missing? import java.awt.*; import java.awt.geom.*; import javax.swing.*; import org.jgraph.*; import org.jgraph.graph.*; import org.jgrapht.*; import org.jgrapht.ext.*; import org.jgrapht.graph.*; // resolve ambiguity import org.jgrapht.graph.DefaultEdge; public class GraphAdapterDemo1 extends JApplet { //~ Static fields/initializers --------------------------------------------- private static final long serialVersionUID = 4049071636005206066L; private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF"); private static final Dimension DEFAULT_SIZE = new Dimension(530, 320); private JGraphModelAdapter<String, DefaultEdge> jgAdapter; public static void main(String [] args) { GraphAdapterDemo1 applet = new GraphAdapterDemo1(); applet.init(); JFrame frame = new JFrame(); frame.getContentPane().add(applet); frame.setTitle("JGraphT Adapter to JGraph Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public void init() { // create a JGraphT graph DirectedGraph<String, DefaultEdge> g = new DirectedMultigraph<String, DefaultEdge>( DefaultEdge.class); // create a visualization using JGraph, via an adapter jgAdapter = new JGraphModelAdapter<String, DefaultEdge>(g); JGraph jgraph = new JGraph(jgAdapter); adjustDisplaySettings(jgraph); getContentPane().add(jgraph); resize(DEFAULT_SIZE); String v1 = "v1"; String v2 = "v2"; String v3 = "v3"; String v4 = "v4"; // add some sample data (graph manipulated via JGraphT) g.addVertex(v1); g.addVertex(v2); g.addVertex(v3); g.addVertex(v4); g.addEdge(v1, v2); g.addEdge(v2, v3); g.addEdge(v3, v1); g.addEdge(v4, v3); // position vertices nicely within JGraph component positionVertexAt(v1, 130, 40); positionVertexAt(v2, 60, 200); positionVertexAt(v3, 310, 230); positionVertexAt(v4, 380, 70); } private void adjustDisplaySettings(JGraph jg) { jg.setPreferredSize(DEFAULT_SIZE); Color c = DEFAULT_BG_COLOR; String colorStr = null; try { colorStr = getParameter("bgcolor"); } catch (Exception e) { } if (colorStr != null) { c = Color.decode(colorStr); } jg.setBackground(c); } @SuppressWarnings("unchecked") // FIXME hb 28-nov-05: See FIXME below private void positionVertexAt(Object vertex, int x, int y) { DefaultGraphCell cell = jgAdapter.getVertexCell(vertex); AttributeMap attr = cell.getAttributes(); Rectangle2D bounds = GraphConstants.getBounds(attr); Rectangle2D newBounds = new Rectangle2D.Double( x, y, bounds.getWidth(), bounds.getHeight()); GraphConstants.setBounds(attr, newBounds); // TODO: Clean up generics once JGraph goes generic AttributeMap cellAttr = new AttributeMap(); cellAttr.put(cell, attr); jgAdapter.edit(cellAttr, null, null, null); } } |