graphstream / gs-core

Graphstream core
http://graphstream-project.org/
Other
402 stars 109 forks source link

Retrieving mouse-clicks of the dynamic nodes in Graphstream #340

Open jxiw opened 4 years ago

jxiw commented 4 years ago

I would like to retrieve nodes in the graph. But those nodes are not created at the beginning. Here are some codes.

initialize

graph = new SingleGraph("Graph");
viewer = graph.display(false);
viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.CLOSE_VIEWER);
viewer.disableAutoLayout();
view = viewer.getDefaultView();
for (MouseListener listener : view.getMouseListeners()) {
    view.removeMouseListener(listener);
}
view.addMouseListener(this);
view.setLayout(null);

// Setup layout
layout = new TreeLayout(graph);

spriteManager = new SpriteManager(graph);
graph.setAttribute("ui.stylesheet", stylesheet);
graph.setAttribute("ui.antialias");
graph.setAttribute("ui.quality");      

add new nodes

Node node = graph.addNode(id);
layout.nodeAdded(id);

mouse click

@Override
public void mouseClicked(MouseEvent e) {
   GraphicElement element = viewer.getDefaultView().findNodeOrSpriteAt(e.getX(), e.getY());
   System.out.println(element.label);
}

The issue is that the element is always null.

Is there anyone know why I can not retrieve those nodes.

Thanks.

hichbra commented 4 years ago

You should implement ViewerListener. You can find an example in swing here, or more here. You can also find an explaination of how it work in the website.

jxiw commented 4 years ago

Still do not work. Here are my code.

public class Visualization implements ViewerListener{

    public void init() {
        // Initialize viewer
        System.setProperty("org.graphstream.ui.renderer",
                "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        graph = new SingleGraph("Graph");
        viewer = graph.display(false);
        viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.CLOSE_VIEWER);
        viewer.disableAutoLayout();
        view = viewer.getDefaultView();
        view.setLayout(null);

        pipeIn = viewer.newViewerPipe();
        pipeIn.addViewerListener(this);
        pipeIn.addSink(graph);
        pipeIn.pump();

        // Setup layout
        layout = new TreeLayout(graph);

        spriteManager = new SpriteManager(graph);
        graph.setAttribute("ui.stylesheet", stylesheet);
        graph.setAttribute("ui.antialias");
        graph.setAttribute("ui.quality");

        // Root node
        addNode("root").addAttribute("ui.label", "Tree");
    }

    @Override
    public void viewClosed(String s) {

    }

    @Override
    public void buttonPushed(String s) {
        System.out.println(s);
    }

    @Override
    public void buttonReleased(String s) {
        System.out.println(s);
    }

    void update() {
          pipeIn.pump();

        Node node = graph.addNode(id);
        layout.nodeAdded(id);

        Edge edge = graph.addEdge(edgeId, fromId, toId, directed);
        layout.edgeAdded(edgeId, fromId, toId, directed);
    }

}
jxiw commented 4 years ago

You should implement ViewerListener. You can find an example in swing here, or more here. You can also find an explaination of how it work in the website.

Is it possible for you to take a look of my code? Thanks.

dmanran commented 4 years ago

@OVSS where call update()