Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

java - GraphStream graph not rendered corectly

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: THe graph

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...