2016-12-10 5 views
0

Je travaille avec la bibliothèque JGraphT pour dessiner un graphique pour ma structure arborescente mais la bibliothèque dessine un graphique basé sur un ensemble donné de nœuds et de bords connectés. J'ai lu la classe "DirectedAcyclicGraph.VisitedArrayListImpl" dans la bibliothèque javadoc mais je Je ne comprends pas très bien et je ne suis pas sûr si c'est ce que je cherche.Comment parcourir une structure arborescente pour dessiner un graphique?

public class JGraphAdapterDemo 
extends JApplet{ 

private static final long serialVersionUID = 3256444702936019250L; 
private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF"); 
private static final Dimension DEFAULT_SIZE = new Dimension(530, 320); 


private JGraphModelAdapter<String, DefaultEdge> jgAdapter; 

/** 
* An alternative starting point for this demo, to also allow running this applet as an 
* application. 
* 
* @param args ignored. 
*/ 
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); 
} 


    {@inheritDoc} 

public void init() 
{ 

    ListenableGraph<String, DefaultEdge> g = 
     new ListenableDirectedMultigraph<>(DefaultEdge.class); 

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

    JGraph jgraph = new JGraph(jgAdapter); 

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

    String v1 = "+"; 
    String v2 = "3"; 
    String v3 = "*"; 
    String v4 = "4"; 
    String v5 = "5"; 

    // add some sample data (graph manipulated via JGraphT) 
    g.addVertex(v1); 
    g.addVertex(v2); 
    g.addVertex(v3); 
    g.addVertex(v4); 
    g.addVertex(v5); 

    g.addEdge(v1, v2); 
    g.addEdge(v1, v3); 
    g.addEdge(v3, v4); 
    g.addEdge(v3, v5); 

    positionVertexAt(v1, 130, 40); 
    positionVertexAt(v2, 50, 150); 
    positionVertexAt(v3, 280, 150); 
    positionVertexAt(v4, 240, 250); 
    positionVertexAt(v5, 400, 250); 

} 

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("unchecked") 
private void positionVertexAt(Object vertex, int x, int y) 
{ 
    DefaultGraphCell cell = jgAdapter.getVertexCell(vertex); 
    AttributeMap attr = cell.getAttributes(); 
    Rectangle2D bounds = GraphConstants.getBounds(attr); 

    Rectangle2D newBounds = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); 

    GraphConstants.setBounds(attr, newBounds); 


    AttributeMap cellAttr = new AttributeMap(); 
    cellAttr.put(cell, attr); 
    jgAdapter.edit(cellAttr, null, null, null); 
} 

private static class ListenableDirectedMultigraph<V, E> 
    extends DefaultListenableGraph<V, E> 
    implements DirectedGraph<V, E> 
{ 
    private static final long serialVersionUID = 1L; 

    ListenableDirectedMultigraph(Class<E> edgeClass) 
    { 
     super(new DirectedMultigraph<>(edgeClass)); 
    } 
}} 

Je suis en utilisant ce code de démonstration de la javadoc et je veux trouver un moyen de se connecter à cette structure d'arbre. Est-ce que quelqu'un a fait quelque chose de similaire?

C'est ma structure d'arbre

public class Tree { 
    Tree left, right; 
char op; 
int val; 
boolean isOp; 

Tree(char op, Tree l, Tree r) { 
this.isOp = true; 
this.left = l; 
this.right = r; 
this.op = op; 
} 

Tree (int v){ 
this.isOp = false; 
this.val = v; 
} 

static void toString(Tree t) { 
if (t.isOp){ 
    toString(t.left); 
    System.out.print(t.op); 
    toString(t.right); 
} 
else 
    System.out.print(t.val); 
} 


public void preorderIter(Tree root) { 

    if(root == null) 
     return; 

    Stack<Tree> stack = new Stack<Tree>(); 
    stack.push(root); 

    while(!stack.empty()){ 

     Tree n = stack.pop(); 
     System.out.printf("%s ",n.op); 


     if(n.right != null){ 
      stack.push(n.right); 
      System.out.printf("%s",n.right); 
     } 
     if(n.left != null){ 
      stack.push(n.left); 
      System.out.printf("%s",n.left); 


     } 

    } 

} 

Je vous serais reconnaissant toute aide

Répondre

0

bibliothèque JGraphT utilise ses propres classes de graphique pour dessiner, de sorte que vous devez soit:

  • abandonner Proposez votre propre arbre d'implémentation et utilisez les classes de la bibliothèque
  • étendez une classe d'arbre appropriée dans la bibliothèque avec votre propre implémentation
  • utiliser votre propre arbre que vous voulez, mais quand vous devez dessiner avec jgraph, copier vos nœuds d'arbres et les arêtes à l'une des implémentations de graphique de cadre
+0

ok, disons que je donne ma bibliothèque d'arbres et je Je vais utiliser les classes de la bibliothèque, une idée sur comment je peux construire un arbre en utilisant les classes JGraphT? une explication serait bien. – Dee

+0

vertex = nœud de l'arbre et bord = connexions entre les nœuds de l'arbre. Sachant cela, vous avez déjà l'exemple de code dans votre premier bloc de code sur la façon de créer un graphique ou un arbre. L'arbre est juste un type particulier de graphique. – Reek