1 25 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 import org.jgrapht.graph.DefaultEdge; 57 58 59 65 public class JGraphAdapterDemo 66 extends JApplet 67 { 68 69 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 77 private JGraphModelAdapter jgAdapter; 79 80 82 88 public static void main(String [] 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 104 public void init() 105 { 106 ListenableGraph<String , DefaultEdge> g = 108 new ListenableDirectedMultigraph<String , DefaultEdge>( 109 DefaultEdge.class); 110 111 jgAdapter = new JGraphModelAdapter<String , DefaultEdge>(g); 113 114 JGraph jgraph = new JGraph(jgAdapter); 115 116 adjustDisplaySettings(jgraph); 117 getContentPane().add(jgraph); 118 resize(DEFAULT_SIZE); 119 120 String v1 = "v1"; 121 String v2 = "v2"; 122 String v3 = "v3"; 123 String v4 = "v4"; 124 125 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 positionVertexAt(v1, 130, 40); 138 positionVertexAt(v2, 60, 200); 139 positionVertexAt(v3, 310, 230); 140 positionVertexAt(v4, 380, 70); 141 142 } 144 145 private void adjustDisplaySettings(JGraph jg) 146 { 147 jg.setPreferredSize(DEFAULT_SIZE); 148 149 Color c = DEFAULT_BG_COLOR; 150 String colorStr = null; 151 152 try { 153 colorStr = getParameter("bgcolor"); 154 } catch (Exception e) { 155 } 156 157 if (colorStr != null) { 158 c = Color.decode(colorStr); 159 } 160 161 jg.setBackground(c); 162 } 163 164 @SuppressWarnings ("unchecked") private void positionVertexAt(Object 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 AttributeMap cellAttr = new AttributeMap(); 182 cellAttr.put(cell, attr); 183 jgAdapter.edit(cellAttr, null, null, null); 184 } 185 186 188 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 <E> edgeClass) 198 { 199 super(new DirectedMultigraph<V, E>(edgeClass)); 200 } 201 } 202 } 203 | Popular Tags |