3

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:

overlapped edges

Fabrizio R.
  • 117
  • 1
  • 14

2 Answers2

6

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 !

Hasal
  • 98
  • 2
  • 6
1

You can solve it by adding the following:

new mxCircleLayout(graph).execute(graph.getDefaultParent());
new mxParallelEdgeLayout(graph).execute(graph.getDefaultParent());
Oleg
  • 6,124
  • 2
  • 23
  • 40
James Adamson
  • 126
  • 1
  • 8
  • StackOverflow is a English site, not Russian. Also how does adding that code fix the users issue. Code only answers are considered low quality ones. – Dijkgraaf Oct 04 '17 at 21:19
  • I translated your answer, please post in English only next time. – Oleg Oct 04 '17 at 22:45