2

I am new to GraphStream and have built a minimal application with a GraphStream 2.0 graph embedded into a Swing application.

In my application, the mouse pointer is apparently offset by about half the panel size (both vertically and horizontically) when I am trying to drag nodes.

This is the code:

package gstest;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.layout.Layout;
import org.graphstream.ui.layout.springbox.implementations.SpringBox;
import org.graphstream.ui.swing_viewer.DefaultView;
import org.graphstream.ui.swing_viewer.SwingViewer;
import org.graphstream.ui.view.Viewer;

public class GSTest {
    
    private static void createAndShowGUI() {

        JFrame frame = new JFrame("GS Test");
        frame.setMinimumSize(new Dimension(1000, 500));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(1, 1));
        
        frame.getContentPane().add(makeMainPanel());
    }
    
    private static JPanel makeMainPanel() {
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        
        Layout graphLayout = new SpringBox(false);
        Graph graph = new SingleGraph("embedded");
        SwingViewer viewer = new SwingViewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        
        JPanel graphPanel = new JPanel();

        DefaultView view = (DefaultView) viewer.addDefaultView(false);
        view.setPreferredSize(new Dimension(980, 460));
        
        graph.addSink(graphLayout);
        graphLayout.addAttributeSink(graph);
        graph.setAttribute("ui.quality");
        graph.setAttribute("ui.antialias");
        
        for (int i = 0; i < 10; i++) {
            Node n = graph.addNode(String.valueOf(i));
            n.setAttribute("ui.style", "shape: box;");
            n.setAttribute("ui.style", "size: 50px,30px;");
            n.setAttribute("ui.style", "fill-color: blue;");
        }
        
        graphLayout.compute();  
        
        graphPanel.add(view);

        return graphPanel;
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

The picture below shows the mouse pointer position when dragging the rectangle node in the center (encircled in red).

enter image description here

Any ideas on the cause of this issue?

Edit:

Here is a short GIF animation showing the problem (the shown application is the code above):

enter image description here

chwon
  • 71
  • 4

2 Answers2

1

You don't use Graphstream 2.0. The generic viewer J2DGraphRenderer :

System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer"); 

Is not used anymore, it's now (depending of your UI):

System.setProperty("org.graphstream.ui", "swing"); //For swing interface
System.setProperty("org.graphstream.ui", "javafx"); //For javafx interface

You can find more info in the official website. And don't forget to check the examples : https://github.com/graphstream/gs-ui-swing/tree/2.0/src-test/org/graphstream/ui/viewer_swing/test

H.Brahimi
  • 254
  • 1
  • 7
  • Thanks for the hint, however it does not affect the described behavior. I changed the system property as you suggested, but is does not have an effect. – chwon Nov 03 '20 at 21:00
  • I tried some samples that you referred to and the mouse offset is present here as well. E.g. the TestFreePlane demo allows to select parts of the graph and shows selected parts in blue color - selection works, but there is again the offset as described, i.e. when selecting a rectangle in the lower right corner of the window frame, a portion of the graph is marked as blue that is roughly in the center of the frame. It seems to be a more fundamental GraphStream issue and not to be related to the specific application. – chwon Nov 03 '20 at 21:08
  • I posted an another answer, that should be it. I don't have issue with TestFreePlane though. Good luck – H.Brahimi Nov 05 '20 at 00:26
1

Ok, I think I understand your problem. It's the same as described here. You replaced the content of the frame by your panel, and due to that the view panel cannot get the right coordinates. This issue is less common in swing though. You can easily prevent that by doing at the end of your function :

graphPanel.add(view);

JPanel content = new JPanel();
content.add(graphPanel);
return content;
H.Brahimi
  • 254
  • 1
  • 7
  • I added an additional enclosing JPanel as you suggested, but there is still the offset when I try to drag a node with the mouse pointer. – chwon Nov 06 '20 at 19:06
  • 1
    I tried the exact same code and it work properly, so I don't know any more, sorry. Try to look your dependencies maybe. – H.Brahimi Nov 06 '20 at 20:30
  • Thanks for your efforts. I will try to run the code on different machines/VMs. (Note: For illustration of the problem I've added an animated GIF in the original question.) – chwon Nov 14 '20 at 13:29