仅在渲染完成时显示图形 - GraphStream JAVA

时间:2017-07-17 06:51:28

标签: java swing rendering graphstream

我正在使用GraphStream库处理java。现在,一切正常,但是当我尝试运行程序时,图形开始在我面前呈现,然后在渲染后停止,当我尝试移动任何节点时,它再次开始渲染,这非常烦人。

我为你们添加了一个sample video here来了解我面临的问题。

我想在完成渲染后显示图形,并且通过移动节点它不应该再次开始渲染。

这是我的代码:

public class GraphExplore {
    static Connection conn2;
    static String result, result2;
    static int totalRows, i = 0;
    static double zoomLevel = 1.0;
    static JTextField Anumber;
    static JButton submit;

    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("GRAPH");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        @SuppressWarnings("serial")
        JPanel panel = new JPanel(new GridLayout()) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1300, 600);
            }
        };
        panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));

        final Graph graph = new SingleGraph("Tutorial", false, true);
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        Anumber = new JTextField(15);
        submit = new JButton("Show Graph");
        Font bigFont = Anumber.getFont().deriveFont(Font.PLAIN, 18f);
        Anumber.setFont(bigFont);
        final ViewPanel viewPanel = viewer.addDefaultView(false);
        viewer.enableAutoLayout();
        viewPanel.add(Anumber);
        viewPanel.add(submit);
        panel.add(viewPanel);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    String Anum = Anumber.getText();
                    Class.forName("org.h2.Driver");
                    conn2 = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa");
                    Statement stmt2 = conn2.createStatement();
                    if (Anum.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "Please enter Anumber");
                    } else {
                        ResultSet rs2 = stmt2.executeQuery(
                                "SELECT ANUMBER,BNUMBER FROM CDR WHERE ANUMBER = '" + Anum + "' ORDER BY ANUMBER");
                        while (rs2.next()) {
                            result = rs2.getString("ANUMBER");
                            result2 = rs2.getString("BNUMBER");
                            graph.addNode(result);
                            graph.addNode(result2);
                            i++;
                            graph.addEdge("string" + i, result, result2);
                            graph.setAutoCreate(true);
                            graph.setStrict(false);
                        }
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        viewPanel.addMouseWheelListener(new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (e.getWheelRotation() == -1) {
                    zoomLevel = zoomLevel - 0.1;
                    if (zoomLevel < 0.1) {
                        zoomLevel = 0.1;
                    }
                    viewPanel.getCamera().setViewPercent(zoomLevel);
                }
                if (e.getWheelRotation() == 1) {
                    zoomLevel = zoomLevel + 0.1;
                    viewPanel.getCamera().setViewPercent(zoomLevel);
                }
            }
        });

        graph.addAttribute("ui.stylesheet", "node {size:8px;fill-color:#ff0000;}");
        // graph.addAttribute("ui.stylesheet", "node:selected {fill-color:#8E44AD;}");
        graph.addAttribute("ui.stylesheet", "edge { shape:angle ;fill-color:#222;}");
        graph.addAttribute("ui.quality");
        graph.addAttribute("ui.antialias");
    }

}

0 个答案:

没有答案