Reputation: 71
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).
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):
Upvotes: 2
Views: 549
Reputation: 263
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;
Upvotes: 1
Reputation: 263
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
Upvotes: 1