2014-03-28 38 views
0

我有问题获取jgrapht或jgraph或applet以正确可视化此图形?我可以使用这个图库来可视化类似于下面的图片吗?例如,U代表x,V代表Y代码。我使用的是使用有向图的演示版本,在本例中也是这样。不知道我是否应该使用jgAdapter或jgxAdapter?目前变得空白的小程序。Java jgraph applet可视化二分图

public class GraphDemo extends JApplet{ 

    private static final long serialVersionUID = 2202072534703043194L; 
     private static final Dimension DEFAULT_SIZE = new Dimension(530, 320); 



     private JGraphXAdapter<String, DefaultEdge> jgxAdapter; 

    public static void main(String[] args) { 



       JGraphAdapterDemo applet = new JGraphAdapterDemo(); 
       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() 
    { 
     UndirectedGraph<String, DefaultEdge> g = 
       new SimpleGraph<String, DefaultEdge>(DefaultEdge.class); 

     jgxAdapter = new JGraphXAdapter<String, DefaultEdge>(g); 

     getContentPane().add(new mxGraphComponent(jgxAdapter)); 
     resize(DEFAULT_SIZE); 

     String x1 = "x1"; 
     String x2 = "x2"; 
     String x3 = "x3"; 

     String y1 = "y1"; 
     String y2 = "y2"; 
     String y3 = "y3"; 
     String y4 = "y5"; 

     g.addVertex(x1); 
     g.addVertex(x2); 
     g.addVertex(x3); 

     g.addVertex(y1); 
     g.addVertex(y2); 
     g.addVertex(y3); 
     g.addVertex(y4); 

     g.addEdge(x1, y1); 
     g.addEdge(x1, y2); 

     g.addEdge(x2, y1); 
     g.addEdge(x2, y4); 

     g.addEdge(x3, y2); 
     g.addEdge(x3, y3); 

     Set<String> p1 = new HashSet<String>(Arrays.asList(x1, x2, x3)); 
     Set<String> p2 = new HashSet<String>(Arrays.asList(y1, y2, y3, y4)); 

     HopcroftKarpBipartiteMatching<String, DefaultEdge> alg = 
      new HopcroftKarpBipartiteMatching<String, DefaultEdge>(g, p1, p2); 

     Set<DefaultEdge> match = alg.getMatching(); 

     mxCircleLayout layout = new mxCircleLayout(jgxAdapter); 
     layout.execute(jgxAdapter.getDefaultParent()); 

     System.out.println(g.toString()); 
     System.out.println(match); 
    } 
} 

enter image description here

回答

0

基于从http://jgrapht.org/visualizations.html的例子,我已经张贴在Java JGrapht Bipartite graph一些方法:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.geom.Rectangle2D; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Random; 

import javax.swing.JApplet; 

import org.jgraph.JGraph; 
import org.jgraph.graph.DefaultGraphCell; 
import org.jgraph.graph.GraphConstants; 
import org.jgrapht.Graph; 
import org.jgrapht.ListenableGraph; 
import org.jgrapht.VertexFactory; 
import org.jgrapht.ext.JGraphModelAdapter; 
import org.jgrapht.graph.DefaultEdge; 
import org.jgrapht.graph.ListenableUndirectedGraph; 

/** 
* Based on the example from http://jgrapht.org/visualizations.html 
*/ 
public class BipartiteGraphVisualizationTest extends JApplet { 
    private static final Color  DEFAULT_BG_COLOR = Color.decode("#FAFBFF"); 
    private static final Dimension DEFAULT_SIZE = new Dimension(800, 600); 

    // 
    private JGraphModelAdapter<String, DefaultEdge> m_jgAdapter; 

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

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

     JGraph jgraph = new JGraph(m_jgAdapter); 

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

     List<String> vertices0 = new ArrayList<String>(); 
     List<String> vertices1 = new ArrayList<String>(); 
     fillGraph(g, vertices0, vertices1); 

     positionVertices(vertices0, vertices1); 
    } 

    private void positionVertices(List<String> vertices0, List<String> vertices1) 
    { 
     int dy0 = DEFAULT_SIZE.height/(vertices0.size() + 2); 
     int y0 = dy0; 
     for (String v0 : vertices0) 
     { 
      positionVertexAt(v0, 100, y0); 
      y0+=dy0; 
     } 
     int dy1 = DEFAULT_SIZE.height/(vertices1.size() + 2); 
     int y1 = dy1; 
     for (String v1 : vertices1) 
     { 
      positionVertexAt(v1, 600, y1); 
      y1+=dy1; 
     } 
    } 


    public static void fillGraph(Graph<String, DefaultEdge> graph, 
     List<String> vertices0, List<String> vertices1) 
    { 
     VertexFactory<String> vertexFactory = new VertexFactory<String>() 
     { 
      int n = 0; 
      @Override 
      public String createVertex() 
      { 
       String s = String.valueOf(n); 
       n++; 
       return s; 
      } 
     }; 
     int numVertices0 = 10; 
     int numVertices1 = 15; 
     int numEdges = 20; 
     generateGraphNoIsolatedVertices(graph, 
      numVertices0, numVertices1, numEdges, 
      vertexFactory, vertices0, vertices1); 
    } 


    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 Rectangle2D.Double(x, y, b.getWidth(), b.getHeight())); 

     Map cellAttr = new HashMap(); 
     cellAttr.put(cell, attr); 
     m_jgAdapter.edit(cellAttr, null, null, null); 
    } 


    // Creates a bipartite graph with the given numbers 
    // of vertices and edges without isolated vertices 
    public static <V, E> void generateGraphNoIsolatedVertices(
     Graph<V, E> graph, int numVertices0, int numVertices1, int numEdges, 
     final VertexFactory<V> vertexFactory, 
     List<V> vertices0, List<V> vertices1) 
    { 
     int minNumEdges = Math.max(numVertices0, numVertices0); 
     if (numEdges < minNumEdges) 
     { 
      System.out.println("At least " + minNumEdges + " are required to " + 
       "connect each of the " + numVertices0 + " vertices " + 
       "to any of the " + numVertices1 + " vertices"); 
      numEdges = minNumEdges; 
     } 

     for (int i = 0; i < numVertices0; i++) 
     { 
      V v = vertexFactory.createVertex(); 
      graph.addVertex(v); 
      vertices0.add(v); 
     } 
     for (int i = 0; i < numVertices1; i++) 
     { 
      V v = vertexFactory.createVertex(); 
      graph.addVertex(v); 
      vertices1.add(v); 
     } 

     // Connect each vertex of the larger set with 
     // a random vertex of the smaller set 
     Random random = new Random(0); 
     List<V> larger = null; 
     List<V> smaller = null; 


     if (numVertices0 > numVertices1) 
     { 
      larger = new ArrayList<V>(vertices0); 
      smaller = new ArrayList<V>(vertices1); 
     } 
     else 
     { 
      larger = new ArrayList<V>(vertices1); 
      smaller = new ArrayList<V>(vertices0); 
     } 
     List<V> unmatched = new ArrayList<V>(smaller); 
     for (V vL : larger) 
     { 
      int i = random.nextInt(unmatched.size()); 
      V vS = unmatched.get(i); 
      unmatched.remove(i); 
      if (unmatched.size() == 0) 
      { 
       unmatched = new ArrayList<V>(smaller); 
      } 
      graph.addEdge(vL, vS); 
     } 

     // Create the remaining edges between random vertices 
     while (graph.edgeSet().size() < numEdges) 
     { 
      int i0 = random.nextInt(vertices0.size()); 
      V v0 = vertices0.get(i0); 
      int i1 = random.nextInt(vertices1.size()); 
      V v1 = vertices1.get(i1); 
      graph.addEdge(v0, v1); 
     } 

    } 

} 
+0

我一直与你的榜样和整合我的数据。我遇到了两个问题,我有大量的数据,并且我也意识到我的数据确实有孤立的顶点以便正确。你以前的功能是否类似于这种情况?另外我想我可以让屏幕尺寸更大来处理大集合? – MAXGEN

+0

当您已经有预定义的数据集时,则不必使用“generateGraph ...”方法。除此之外:我提供了大量完美运行的例子来回答你的问题。如果您正在寻找为您编写完整应用程序的人,那么您不应该在Q/A网站上看到像stackoverflow那样的网站,而应该在职位交换网站上查看。 – Marco13

+0

无论哪种方式感谢您花时间和回答。你有什么建议吗?你有没有参考他们中的任何一个? – MAXGEN