所有节点聚集在一个地方 - GraphStream - JAVA

时间:2017-07-13 05:39:46

标签: java swing jpanel graphstream

我正在使用GraphStream Library for java。但我在这里面临一个问题。 当我尝试从数据库表中获取节点时,它会在JPanel上显示数据和边缘,但首先,当程序启动时,它会将所有节点聚集在一个地方,如此图像。所以,我必须拖动所有节点。 为什么会这样?任何一点点提示都会受到赞赏。

enter image description here

这是我的代码:

public class GraphExplore {
    static Connection conn2;
    static String result, result2;
    static int totalRows, i;

    public static void main(String args[]) throws SQLException {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    showData();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static void showData() throws SQLException {

        JFrame frame = new JFrame();
        frame.setBounds(30, 50, 1300, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridLayout()){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));
        Graph graph = new SingleGraph("Tutorial", false, true);

        try {
            Class.forName("org.h2.Driver");
            conn2 = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa");

        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement stmt2 = conn2.createStatement();
        ResultSet rs = stmt2.executeQuery("SELECT count(*) FROM cdr");
        while (rs.next()) {
            totalRows = rs.getInt(1);
        }
        ResultSet rs2 = stmt2.executeQuery("SELECT ANUMBER,BNUMBER FROM CDR LIMIT 1000");
        while (rs2.next()) {
            result = rs2.getString("ANUMBER");
            result2 = rs2.getString("BNUMBER");
            graph.addNode(result);
            graph.addNode(result2);
            for (i = 0; i < 1000; i++)
                graph.addEdge("string" + i, result, result2);
        }

        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        ViewPanel viewPanel = viewer.addDefaultView(false);
        panel.add(viewPanel);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        for (Node node : graph) {
            node.addAttribute("ui.label", node.getId());
        }

        }

}

在此之前我使用graph.display()并且它工作正常但是当我将图形面板添加到我的自定义JPanel时,它开始表现得像这样。

2 个答案:

答案 0 :(得分:1)

试试这个:

   viewer.enableAutoLayout();

答案 1 :(得分:0)

在取消自动版式布局后尝试以下功能:

//desable autolayout
viewer.disableAutoLayout();

//set a random node positions
setNodePositions();

函数setNodePositions()的定义如下:

private void setNodePositions(){       
   Random r = new Random();
   for(Node node:graph.getNodeSet()){
       if(!node.hasAttribute("xyz")){
           node.addAttribute("xyz",r.nextInt(x_max), r.nextInt(y_max), 0);
       }
   }                                
}

x_max和y_max是面板的宽度和高度。