2014-07-01 8 views
6

Ho un SimpleWeightedGraph e voglio disegnarlo su un JPanel in un JFrame. Purtroppo non viene disegnato nulla.Disegno di un SimpleWeightedGraph su un JPanel

Ho letto this article. Stanno usando un ListenableDirectedGraph quindi ho provato uno ListenableUndirectedGraph senza successo.

public class DisplayGraphForm extends javax.swing.JFrame { 
    public DisplayGraphForm(SimpleWeightedGraph g) { 
     initComponents(); // graphPanel added to JFrame with BorderLayout (Center) 

     JGraphModelAdapter adapter = new JGraphModelAdapter(g); 

     JGraph jgraph = new JGraph(adapter); 
     graphPanel.add(jgraph); 
    } 
} 

risposta

12

E sembra che si sta lasciando alcuni dettagli importanti fuori della tua domanda, e senza un Minimal, Complete, and Verifiable example è difficile dire dove è il problema.

Tuttavia, si noti che l'esempio che si sta tentando di adottare è molto vecchio. JGraph è stato spostato su JGraphX. Si consideri il seguente esempio che dimostra il collegamento di JGraphT e JGraphX utilizzando JGraphXAdapter.

import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

import org.jgrapht.ListenableGraph; 
import org.jgrapht.ext.JGraphXAdapter; 
import org.jgrapht.graph.DefaultWeightedEdge; 
import org.jgrapht.graph.ListenableDirectedWeightedGraph; 
import com.mxgraph.layout.mxCircleLayout; 
import com.mxgraph.layout.mxIGraphLayout; 
import com.mxgraph.swing.mxGraphComponent; 

public class DemoWeightedGraph { 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("DemoGraph"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     ListenableGraph<String, MyEdge> g = buildGraph(); 
     JGraphXAdapter<String, MyEdge> graphAdapter = 
       new JGraphXAdapter<String, MyEdge>(g); 

     mxIGraphLayout layout = new mxCircleLayout(graphAdapter); 
     layout.execute(graphAdapter.getDefaultParent()); 

     frame.add(new mxGraphComponent(graphAdapter)); 

     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 

    public static class MyEdge extends DefaultWeightedEdge { 
     @Override 
     public String toString() { 
      return String.valueOf(getWeight()); 
     } 
    } 

    public static ListenableGraph<String, MyEdge> buildGraph() { 
     ListenableDirectedWeightedGraph<String, MyEdge> g = 
      new ListenableDirectedWeightedGraph<String, MyEdge>(MyEdge.class); 

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

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

     MyEdge e = g.addEdge(x1, x2); 
     g.setEdgeWeight(e, 1); 
     e = g.addEdge(x2, x3); 
     g.setEdgeWeight(e, 2); 

     e = g.addEdge(x3, x1); 
     g.setEdgeWeight(e, 3); 

     return g; 
    } 
} 

enter image description here

noti che MyEdge estende DefaultWeightedEdge fornire personalizzato toString() che visualizza peso bordo. Una soluzione più pulita sarebbe probabilmente quella di ignorare lo mxGraph.convertValueToString, esaminare il contenuto delle celle e fornire etichette personalizzate secondo necessità. toString è una scorciatoia per la demo e inoltre ho notato che DefaultWeightedEdge.getWeight() è protetto, quindi l'estensione è comunque necessaria :)