GraphStream & Swing - 无法正确显示我的图表

GraphStream & Swing - Can't display my graph correctly

我是 GraphStream 和 Swing 的初学者,我想在 JPanel 中插入一个图表,但在尝试时我得到以下显示:

Main Frame

我在代码中添加了一个 graph.display() 以查看问题是否来自图表但它似乎正确显示:

The graph with graph.display()

我的印象是当图表在面板中时,顶点不想正确定位自己。

代码:

我的自定义面板:

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);
    }
}

我的相框:

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 :我在 Automatic layout 部分的 graphstream (https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/1.0/) 文档中找到了解决方案,当使用查看器时,您必须添加viewer.enableAutoLayout(); 才能正确显示

带有解决方案的新代码:

我的自定义面板:

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);
    }
}

我在自动布局部分的 graphstream (https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/1.0/) 文档中找到了解决方案,当使用查看器时,您必须添加 viewer.enableAutoLayout();正确显示