Undirected Graphs in JGraphX

坚强是说给别人听的谎言 提交于 2020-01-23 12:14:55

问题


I try to show graphs in JGraphx. Everything is fine as long as I use directed Graphs, but when I try to show an undirected one, its shown with direction.

The code is from the demo of jgrapht.


package org.jgrapht.demo;

import com.mxgraph.layout.*;
import com.mxgraph.swing.*;

import java.awt.*;
import java.nio.file.FileSystem;

import javax.swing.*;

import org.jgrapht.*;
import org.jgrapht.ext.*;
import org.jgrapht.graph.*;


/**
 * A demo applet that shows how to use JGraphX to visualize JGraphT graphs.
 * Applet based on JGraphAdapterDemo.
 *
 * @since July 9, 2013
 */
public class JGraphXAdapterDemo
    extends JApplet
{


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



private JGraphXAdapter<String, DefaultEdge> jgxAdapter;



/**
 * 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()
{
    // create a JGraphT graph
    ListenableUndirectedGraph<String, DefaultEdge> g =
        new ListenableUndirectedGraph<String, DefaultEdge>(
            DefaultEdge.class);

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

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

    String v1 = "v1";
    String v2 = "v2";
    String v3 = "v3";
    String v4 = "v4";

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

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

    // positioning via jgraphx layouts
    mxCircleLayout layout = new mxCircleLayout(jgxAdapter);
    layout.execute(jgxAdapter.getDefaultParent());

    // that's all there is to it!...

   }
}

Is it possible to show it undirected?

Thanks

Torben

PS.: Sorry for my bad English


回答1:


You can set the style of the edges in the mxGraphComponent to NONE:

mxGraphComponent graphComponent = new mxGraphComponent(jgxAdapter);
mxGraphModel graphModel  = (mxGraphModel)graphComponent.getGraph().getModel(); 
Collection<Object> cells =  graphModel.getCells().values(); 
mxUtils.setCellStyles(graphComponent.getGraph().getModel(), 
    cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE);
//instead of getContentPane().add(new mxGraphComponent(jgxAdapter));
getContentPane().add(graphComponent); 



回答2:


for someone else have the same problem take look for this code

import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxUtils;
import java.util.Collection;
import javax.swing.JFrame;

import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;

public class GraphTest extends JFrame {

  public GraphTest(){

      JGraphXAdapter<String, DefaultEdge> jgxAdapter;
      UndirectedGraph<String, DefaultEdge> graph =
      new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);


    graph.addVertex("v1");
    graph.addVertex("v2");
    graph.addVertex("v3");
    graph.addVertex("v4");

    graph.addEdge("v1", "v2");
    graph.addEdge("v2","v3");
    graph.addEdge("v3", "v4");
    graph.addEdge("v4", "v1");


    jgxAdapter = new JGraphXAdapter <String, DefaultEdge>(graph);
    mxGraphComponent graphComponent = new mxGraphComponent(jgxAdapter);
    mxGraphModel graphModel  =       (mxGraphModel)graphComponent.getGraph().getModel(); 
    Collection<Object> cells =  graphModel.getCells().values(); 
    mxUtils.setCellStyles(graphComponent.getGraph().getModel(), 
    cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE);
    getContentPane().add(graphComponent);

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

}
  public static void main(String[] args) {

     GraphTest g = new GraphTest();

          g.setTitle(" undirected graph ");
          g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          g.pack();
          g.setVisible(true);
  }

}


来源:https://stackoverflow.com/questions/26084742/undirected-graphs-in-jgraphx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!