How to add a node at cursor position to GraphStream graph on click?

898 Views Asked by At

I am developing a Java Swing application to display a graph using GraphStream. When clicking on the graph component (a ViewPanel) a node should appear where the click has happened.

This basically works but adding nodes close to the edges of the ViewPanel creates the visual effect of zooming out of the graph. Also one of the first few clicks somehow changes the camera's center and/or zoom.

I followed the section "Working in the GUI thread" of library's documentation. Thus I am not using a ViewerListener.

To make sure the layout is not automatic I use:

viewer.disableAutoLayout();
view.getCamera().setAutoFitView(false);
view.getCamera().setViewCenter(0, 0, 0);
view.getCamera().setViewPercent(1);

Mouse events are handled like so:

view.addMouseListener(new MouseListener() {
    public void mouseClicked(MouseEvent e) {
        Point3 gu = view.getCamera().transformPxToGu(e.getX(), e.getY());
        Node node = graph.addNode(e.getWhen());
        node.setAttribute("xyz", gu.x, gu.y, 0);
        // System.out.println("camera: " + view.getCamera());
    }
});

Printing the camera object showed that there was a padding of 30px. I thought the padding causes the problem (it seemed reasonable that enforcing the padding could shrink the graph if something is added in the padding area). By creating a subclass of the DefaultCamera class I could get rid of the padding - but that didn't change anything.

Any ideas why new nodes can't be created at the edges?

Thanks in advance!

0

There are 0 best solutions below