Overlapping edges with JGraphx

老子叫甜甜 提交于 2020-12-31 06:22:31

问题


I'm building a graph with JgraphX. The directed graph represent roads and intersections. For every route I define two edges, one for each direction.

As result, the image of the graph has the two edges (representing the road) overlapped. How can I avoid this? Does the vertex have some things like anchor points for the edges? If so, how can I define them?

This is the code I use to display graph

package it.rex.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jgrapht.ListenableGraph;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.ListenableDirectedGraph;

import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.swing.mxGraphComponent;

//import grafotest1.DemoWeightedGraph.MyEdge;
import it.rex.model.Incrocio;
import it.rex.model.Strada;
import javax.swing.JScrollPane;
public class StradarioView extends JFrame {



    /**
     * Create the frame.
     */
    public StradarioView(ListenableGraph<Incrocio, Strada> listenableGraph) {

        // Graph come from JgraphT
        JGraphXAdapter<Incrocio, Strada> graphAdapter = 
                new JGraphXAdapter<Incrocio, Strada>(listenableGraph);

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

        mxGraphComponent graphComponent = new mxGraphComponent(graphAdapter);
        getContentPane().add(graphComponent, BorderLayout.CENTER);

        setTitle("Stradario");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        pack();


        add(graphComponent);
    }
}

This the result with overlapped edges:


回答1:


You have used the Circle Layout here.

mxIGraphLayout layout = new mxCircleLayout(graphAdapter);

Circle Layout places the nodes in a circle but it overlaps the edges between two nodes which are in opposite direction. Instead you can try using the mxParallelEdgeLayout. This will separate the two edges if they are overlapping.

Try this:

mxParallelEdgeLayout layout = new mxParallelEdgeLayout(graphAdapter);

The following picture shows two connected nodes which has two edges between e and b, which are in opposite directions. That is how the mxParallelEdgeLayout displays two edges between the same two nodes in opposite directions

Hope this helps !




回答2:


You can solve it by adding the following:

new mxCircleLayout(graph).execute(graph.getDefaultParent());
new mxParallelEdgeLayout(graph).execute(graph.getDefaultParent());


来源:https://stackoverflow.com/questions/41862339/overlapping-edges-with-jgraphx

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