I'm working with GraphStream and I can't get the mouse pointer to drag nodes correctly.
(Note how the mouse offset gets worse towards the bottom right corner).
This looks like exactly the same problem as here (similar to this, only for Swing instead of JavaFX).
I tried everything I could think of, read the GraphStream docs, FAQ, checked the tests, etc.
I also tried the suggested solution here, but it doesn't work - the behavior is exactly the same.
Here's my code (after applying the suggestion):
DefaultView view = (DefaultView) viewer.addView("Graph", new SwingGraphRenderer(), false);
view.setLayout(new BorderLayout());
view.setPreferredSize(frame.getSize());
view.setMouseManager(new DefaultMouseManager());
JPanel panel = new JPanel();
panel.add(view);
frame.add(panel);
view.setVisible(true);
panel.setVisible(true);
Here is the code for the full (minimalistic) example:
package main.java;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.ui.swing.SwingGraphRenderer;
import org.graphstream.ui.swing_viewer.DefaultView;
import org.graphstream.ui.swing_viewer.SwingViewer;
import org.graphstream.ui.swing_viewer.util.DefaultMouseManager;
import org.graphstream.ui.view.Viewer;
import javax.swing.*;
import java.awt.*;
public class Example {
public static void main(String[] args) {
System.setProperty("org.graphstream.ui", "swing");
Graph g = new DefaultGraph("Graph");
Node n1 = g.addNode("V1");
Node n2 = g.addNode("V2");
g.addEdge("E1", "V1", "V2");
String style = "size: 30px; stroke-mode: plain; fill-color: rgba(223, 187, 254, 255);";
n1.setAttribute("ui.style", style);
n2.setAttribute("ui.style", style);
// g.display(); <- same behavior as dropping everything below this line.
JFrame frame = new JFrame();
frame.setBounds(0, 0, 600, 600);
frame.setLayout(new BorderLayout());
SwingViewer viewer = new SwingViewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
viewer.enableAutoLayout();
DefaultView view = (DefaultView) viewer.addView(g.getId(), new SwingGraphRenderer(), false);
view.setLayout(new BorderLayout());
view.setPreferredSize(frame.getSize());
JPanel panel = new JPanel();
panel.add(view);
frame.add(panel);
view.setMouseManager(new DefaultMouseManager());
view.setVisible(true);
panel.setVisible(true);
frame.setVisible(true);
}
}
And the pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-core</artifactId>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.graphstream/gs-ui-swing -->
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-ui-swing</artifactId>
<version>2.0</version>
</dependency>
</dependencies>