GraphStream graph not rendered corectly

前提是你 提交于 2019-12-12 02:28:08

问题


I have the following code while trying to display a graph in a form. For 3 nodes the code works well but after adding other nodes the graph is not showed entirely. I have no idea where the problem can be.

public class Test{
public static void main(String args[]) throws IOException{

    final DefaultGraph g = new DefaultGraph("my beautiful graph");
g.setStrict(false);
    Viewer viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
    JFrame myJFrame = new JFrame();
    myJFrame.setPreferredSize(new Dimension(600,600));
    SwingBasicGraphRenderer renderer = new SwingBasicGraphRenderer();
    DefaultView view = new DefaultView(viewer, "MyView", renderer);
    view.setPreferredSize(new Dimension(400,400));
    myJFrame.setLayout(new FlowLayout());
    myJFrame.add(view);
    JButton myButton = new JButton("MyButton");
    myButton.addActionListener(e -> System.out.println("Somebody pushed my button."));
    myJFrame.add(myButton);
    myJFrame.pack();
    myJFrame.setVisible(true);
    viewer.enableAutoLayout();
    ViewerPipe vp = viewer.newViewerPipe();
    vp.addViewerListener(new ViewerListener() {
        @Override
        public void viewClosed(String viewName) {
            // dont care
        }

        @Override
        public void buttonPushed(String id) {
            Node n = g.getNode(id);
            String attributes[] = n.getAttributeKeySet().toArray(new String[n.getAttributeKeySet().size()]);

            String attributeToChange = (String) JOptionPane.showInputDialog(null, "Select attibute to modify", "Attribute for " + id, JOptionPane.QUESTION_MESSAGE, null, attributes, attributes[0]);
            String curValue = n.getAttribute(attributeToChange);
            String newValue
                    = JOptionPane.showInputDialog("New Value", curValue);
            n.setAttribute(attributeToChange, newValue);
        }

        @Override
        public void buttonReleased(String id) {
            // don't care
        }
    });
    g.addNode("A");
    g.addNode("B");
    g.addNode("C");

    g.addNode("E");
    g.addNode("F");
    g.addNode("G");

    g.addNode("1");
    g.addNode("2");
    g.addNode("3");

    g.addNode("4");
    g.addNode("5");
    g.addNode("6");



    g.addEdge("AB", "A", "B");
    g.addEdge("AC", "B", "C");
    g.addEdge("BC", "C", "C");

    g.addEdge("EB", "E", "B");
    g.addEdge("FC", "F", "C");
    g.addEdge("GC", "G", "C");

    g.addEdge("1B", "1", "B");
    g.addEdge("2C", "2", "C");
    g.addEdge("3C", "3", "C");

    g.addEdge("4B", "4", "B");
    g.addEdge("5C", "5", "C");
    g.addEdge("6C", "6", "C");




    g.getNode("A").setAttribute("size", "big");
    g.getNode("B").setAttribute("size", "medium");
    g.getNode("C").setAttribute("size", "small");
    g.getNode("A").setAttribute("color", "red");
    g.getNode("B").setAttribute("color", "blue");
    g.getNode("C").setAttribute("color", "green");
    while (true) {
        (view).repaint();
        vp.pump();
    }
}
}

The graph:


回答1:


Since it seems to move unpredictably the best I can do for now is to zoom out so everything will be in view.

view.getCamera().setViewPercent(5.0);

Update:

It seems to work better to cast viewrt.getDefaultView() to a DefaultView rather than calling the DefaultVeiw constructor directly.

change

SwingBasicGraphRenderer renderer = new SwingBasicGraphRenderer();
DefaultView view = new DefaultView(viewer, "MyView", renderer);

to

DefaultView view = (DefaultView) viewer.addDefaultView(false);


来源:https://stackoverflow.com/questions/34287865/graphstream-graph-not-rendered-corectly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!