0

I'm a beginner with GraphStream and Swing, I would like to insert a Graph in a JPanel, but while trying I get the following display:

Main Frame

I added a graph.display() in the code to see if the problem came from the graph but it seems to display correctly:

The graph with graph.display()

I have the impression that when the graph is in the panel the vertices do not want to position themselves correctly.

Code:

My custom panel:

import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import javax.swing.*;
import java.awt.*;

public class PanelGraph extends JPanel {

    public SingleGraph graph = new SingleGraph("Test graph");

    public PanelGraph() {

        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addEdge("AB", "A", "B");
        graph.addEdge("BC", "B", "C");
        graph.addEdge("CA", "C", "A");

        graph.setStrict(false);
        graph.setAutoCreate(true);

        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        View view = viewer.addDefaultView(false);
        setLayout(new BorderLayout());
        add((Component) view, BorderLayout.CENTER);
    }
}

My frame:

import javax.swing.*;
import java.awt.*;

public class TestFrame extends JFrame {

    public TestFrame() {
        super("Test frame");
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
    }

    private void init() {
        JPanel panelInputs = new JPanel();
        JPanel panelSide = new JPanel();
        PanelGraph panelGraph = new PanelGraph(); // The graph is in this panel

        panelInputs.add(new JLabel("Inputs panel"));
        panelSide.add(new JLabel("Side panel"));

        this.getContentPane().setLayout(new GridBagLayout());
        this.getContentPane().add(panelInputs, new GridBagConstraints(0, 0, 3, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        this.getContentPane().add(panelSide, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        this.getContentPane().add(panelGraph, new GridBagConstraints(1, 1, 2, 1, 2, 2, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        this.setPreferredSize(new Dimension(1600, 900));
        this.pack();
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        TestFrame frame = new TestFrame();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

EDIT : I found the solution on the documentation of graphstream (https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/1.0/) in the Automatic layout section, when using a viewer you have to add viewer.enableAutoLayout(); to display it correctly

New code with the solution:

My custom panel:

import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import javax.swing.*;
import java.awt.*;

public class PanelGraph extends JPanel {

    public SingleGraph graph = new SingleGraph("Test graph");

    public PanelGraph() {

        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addEdge("AB", "A", "B");
        graph.addEdge("BC", "B", "C");
        graph.addEdge("CA", "C", "A");

        graph.setStrict(false);
        graph.setAutoCreate(true);

        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        viewer.enableAutoLayout();
        View view = viewer.addDefaultView(false);
        setLayout(new BorderLayout());
        add((Component) view, BorderLayout.CENTER);
    }
}
  • You should set a `BorderLayout` instead of a `BoxLayout`. – Olivier Jan 22 '22 at 11:27
  • I tried with the new code updated in my post, I change to `setLayout(new BorderLayout());` but it doesn't seems to change anything... Am I instancing it wrong? I just noticed too by moving the vertices that the graph is correctly created, the problem is the automatic arrangement of the vertices which is not done correctly. Any ideas ? Thanks – BenoitPEGAZ Jan 22 '22 at 22:26

1 Answers1

2

I found the solution on the documentation of graphstream (https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/1.0/) in the Automatic layout section, when using a viewer you have to add viewer.enableAutoLayout(); to display it correctly

  • 1
    I'm trying to run your code but I'm getting an error from this line `Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);` because **'Viewer' is abstract; cannot be instantiated** – Kfir Ettinger May 04 '22 at 11:15
  • As i see here https://graphstream-project.org/gs-core/org/graphstream/ui/view/Viewer.html and here https://data.graphstream-project.org/api/gs-core/current/org/graphstream/ui/view/Viewer.html , the Viewer class is not the same between version 1.3 and later versions of Graphstream.My code snippet uses Graphstream 1.3 – BenoitPEGAZ May 04 '22 at 12:42