【问题标题】:GraphStream & Swing - Can't display my graph correctlyGraphStream & Swing - 无法正确显示我的图表
【发布时间】:2022-01-22 11:11:35
【问题描述】:

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

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

带有解决方案的新代码:

我的自定义面板:

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

【问题讨论】:

  • 您应该设置BorderLayout 而不是BoxLayout
  • 我尝试在我的帖子中更新新代码,我更改为setLayout(new BorderLayout());,但它似乎没有改变任何东西......我实例化它错了吗?我刚刚也注意到通过移动正确创建图形的顶点,问题是顶点的自动排列没有正确完成。有任何想法吗 ?谢谢

标签: java swing view render graphstream


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2018-02-08
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2019-01-24
  • 1970-01-01
  • 2020-08-27
相关资源
最近更新 更多