2014-03-28 63 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