Updated version of JGraphAdapterDemo (JGraph + JGraphT)

Submitted by admin on Thu, 01/29/2015 - 15:07

Here an updated version (2015) of demo published in <a href="http://jgrapht.org/visualizations.html">http://jgrapht.org/visualizatio…;

<code>
package org.jgrapht.demo;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JApplet;
import javax.swing.JFrame;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgrapht.ListenableGraph;
import org.jgrapht.ext.JGraphModelAdapter;
import org.jgrapht.graph.ListenableDirectedGraph;
import org.jgrapht.graph.DefaultEdge;

/**
* A demo applet that shows how to use JGraph to visualize JGraphT graphs.
*
* @author Barak Naveh
*
* @since Aug 3, 2003
*/
public class JGraphAdapterDemo extends JApplet {
private static final Color DEFAULT_BG_COLOR = Color.decode( "#FAFBFF" );
private static final Dimension DEFAULT_SIZE = new Dimension( 530, 320 );

//
private JGraphModelAdapter m_jgAdapter;

/**
* @see java.applet.Applet#init().
*/
public void init( ) {
// create a JGraphT graph
ListenableGraph g = new ListenableDirectedGraph( DefaultEdge.class );

// create a visualization using JGraph, via an adapter
m_jgAdapter = new JGraphModelAdapter( g );

JGraph jgraph = new JGraph( m_jgAdapter );

adjustDisplaySettings( jgraph );
getContentPane( ).add( jgraph );
resize( DEFAULT_SIZE );

// 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 );

// that's all there is to it!...
}

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 );
}

private void positionVertexAt( Object vertex, int x, int y ) {
DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
Map attr = cell.getAttributes( );
Rectangle2D b = GraphConstants.getBounds( attr );

GraphConstants.setBounds( attr, new Rectangle( x, y, b.OUT_RIGHT, b.OUT_TOP) );

Map cellAttr = new HashMap( );
cellAttr.put( cell, attr );

m_jgAdapter.edit( cellAttr, null, null, null );
}
}
</code>

Maven sources

<code>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-jdk1.5</artifactId>
<version>0.7.3</version>
</dependency>

<dependency>
<groupId>jgraph</groupId>
<artifactId>jgraph</artifactId>
<version>5.13.0.0</version>
</dependency>
</code>

Etiquetas