0

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

主机架

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

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

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

4

1 回答 1

1

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

于 2022-01-25T14:31:13.333 回答