how to use static layout in graphstream

≡放荡痞女 提交于 2019-12-11 03:05:03

问题


I would like to generate static graph use GraphStream. Is there any suggestion solutions?

I read input data from DGS file (generated from other model). Because the number of nodes is not fix every time and I don't want to manually specify each position of each node. Currently I can use the autolayout function to place the nodes and the result is OK, but the problem is: each node can be moved if you click the node and drag it. Is there any way to generate the graph use autolayout, and the node is un-draggable at the same time?

Thanks in advance!


回答1:


In order to prevent user interaction with the default viewer, you need to specify a new MouseManager, in the default View, that actually does nothing.

Since you cannot set a null MouseManager (it would spawn the default one), you need to create a new class that basically does nothing and give it to the View.

A dirty way of doing it:

graph.display()
    .getDefaultView()
    .setMouseManager(new MouseManager(){
        @Override
        public void mouseClicked(MouseEvent e) {}
        @Override
        public void mousePressed(MouseEvent e) {}
        @Override
        public void mouseReleased(MouseEvent e) {}
        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}
        @Override
        public void mouseDragged(MouseEvent e) {}
        @Override
        public void mouseMoved(MouseEvent e) {}
        @Override
        public void init(GraphicGraph graph, View view) {}
        @Override
        public void release() {}
    });



回答2:


This is inspired by @Yoann answer, but its a better way of performing the job. If you just want to strict some of the mouse activity, you can do following:

ViewPanel viewPanel = graph.display().getDefaultView();
MouseManager manager = new DefaultMouseManager() {

    @Override
    public void mouseDragged(MouseEvent event) {

    }

    @Override
    protected void mouseButtonPress(MouseEvent event) {
        super.mouseButtonPress(event);

        System.out.println("Press");
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        super.mouseClicked(event);
        System.out.println("Clicked");
    }

    @Override
    public void mousePressed(MouseEvent event) {
        super.mousePressed(event);

        // if you need object of Node pressed, following code will help you, curElement is already defined at DefaultMouseManager.
        curElement = view.findNodeOrSpriteAt(event.getX(), event.getY());

        if (curElement != null) {
            Node node = graph.getNode(curElement.getId());
            if(node != null) {
                System.out.println("Mouse pressed at node: " + node.getId());
            }
        }

    }

};

viewPanel.setMouseManager(manager);

Here, I have disabled mouseDragged, and writing my own code after performing default mouseButtonPress, mouseClicked and mousePressed event. You can even create your own Class which extends DefaultMouseManaer and override the function you want and set that object to mouse manager, this will be more clear way of programming.



来源:https://stackoverflow.com/questions/28458212/how-to-use-static-layout-in-graphstream

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