先转了别人的代码,等完全搞明白,可能在做总结吧

graph visualization with Java

A couple of years ago I came across a Java-based library for graph visualization – JGraph/JGraphT. Recently, I had the chance to play with it in a real context.

Here is a short step-by-step guide to create a simple applet:

  1. Create a Java-project in eclipse
  2. Download the library from http://jgrapht.org/ and extract it
  3. Add ‘jgrapht-jdk1.6.jar’ to the project’s build path
  4. Add a new class ‘GraphTest.java’ to the project.
  5. Copy the code below into ‘GraphTest.java’ (...or download the attached file)
  6. Run as applet
package com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.Rectangle2D;

import java.util.HashMap;
import java.util.Map;
import javax.swing.JApplet;
import javax.swing.undo.UndoableEdit;

import org.jgraph.JGraph;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.ParentMap;

import org.jgrapht.ListenableGraph;
import org.jgrapht.ext.JGraphModelAdapter;
import org.jgrapht.graph.ListenableDirectedGraph;
import org.jgrapht.graph.DefaultEdge;

@SuppressWarnings("serial")
/**
 * A demo applet that shows how to use JGraph to visualize JGraphT graphs.
 *
 * @author Barak Naveh, edited by Martin Bäumer in Feb 2011
 *
 * @since Aug 3, 2003
 */
public class GraphTest extends JApplet {
    
	private static final Color     DEFAULT_BG_COLOR = Color.decode( "#FAFBFF" );
    private static final Dimension DEFAULT_SIZE = new Dimension( 530, 320 );
     
    @SuppressWarnings("rawtypes")
	private JGraphModelAdapter jgAdapter;
        
    @SuppressWarnings({ "unchecked", "rawtypes" })
    /**
     * Initialize the applet @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
        jgAdapter = new JGraphModelAdapter( g );

        JGraph jgraph = new JGraph( jgAdapter );

        // add some hard-coded sample data (graph manipulated via JGraphT)
        //First the vertices
        g.addVertex( "v1" );
        g.addVertex( "v2" );
        g.addVertex( "v3" );
        g.addVertex( "v4" );

        //...then the edges
        g.addEdge( "v1", "v2" );
        g.addEdge( "v2", "v3" );
        g.addEdge( "v3", "v1" );
        g.addEdge( "v4", "v3" );           
        
        // a ConnectionSet is another way of storing edges. Each connection stores information about the edge's nodes and its origin/source
        // This ConnectionSet is necessary for the visualization 
        ConnectionSet cs=new ConnectionSet();
        cs.connect("v1", "v2", true);
        cs.connect("v2", "v3", true);
        cs.connect("v3", "v1", true);
        cs.connect("v4", "v3", true);
        
        adjustDisplaySettings( jgraph );
        getContentPane(  ).add( jgraph );
        resize( DEFAULT_SIZE );

        // position vertices nicely within JGraph component
        positionVertexAt( "v1", 130, 40, cs );
        positionVertexAt( "v2", 60, 200, cs );
        positionVertexAt( "v3", 310, 230, cs );
        positionVertexAt( "v4", 380, 70, cs );
    }

    /**
     * a method for general settings of the applet
     * @param jg
     */
    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({ "rawtypes", "unchecked" })
    /**
     * Set a position for the vertices
     */
	private void positionVertexAt( Object vertex, int x, int y , ConnectionSet cs) {
        DefaultGraphCell cell = jgAdapter.getVertexCell( vertex );
        AttributeMap vertexAttributes=cell.getAttributes();
        Rectangle2D rect=vertexAttributes.createRect(new Point(x,y),50);
        
        Map attr = cell.getAttributes();      
        
        GraphConstants.setBounds( attr,rect);

        Map cellAttr = new HashMap(  );
        cellAttr.put( cell, attr );
        UndoableEdit[] e=null;

        ParentMap m=new ParentMap();
        jgAdapter.edit(cellAttr, cs, m, e);
    }
}