Draw/Add rectangles/other shapes in the Viewer
Closed this issue · 3 comments
Is there a way to add objects to the Viewer component like a rectangle or a polygon. I want to draw a rectangle with co-ordinates as input around a group of nodes (whose co-ordinates are manually set, not automatic layout) in the Viewer? Does Graphstream GUI currently handle this?
**NOTE: This is not an issue per se, but would be a nice-to-have feature if not already implemented.
You can get the panel from the viewer, with that you can draw what you want. If you don't build your own view, you can get the viewer from the display method. Here a little example
public TestDraw() {
System.setProperty("org.graphstream.ui", "swing"); // With Graphstream 2.0
Graph graph = new MultiGraph("Graph");
Node node1 = graph.addNode("1");
Node node2 = graph.addNode("2");
Node node3 = graph.addNode("3");
Node node4 = graph.addNode("4");
node1.setAttribute("xyz", new double[] { 0, 0, 0 });
node2.setAttribute("xyz", new double[] { 0, 10, 0 });
node3.setAttribute("xyz", new double[] { 10, 0, 0 });
node4.setAttribute("xyz", new double[] { 10, 10, 0 });
// Get the panel
Viewer viewer = graph.display(false);
DefaultView panel = (DefaultView) viewer.getDefaultView();
// Allow time for the viewer to build
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Draw with graphics
panel.getGraphics().drawRect(120, 20, 550, 530);
}
Keep in mind that any action will redraw your graph, thus you will may need to redraw your view with a listener
@hichbra Thanks for the example - just one additional question though - I checked the methods that correlate Graph units to pixel units/positions - what is the exact method to call that can get the node positions and draw rectangles using the node positions ?
Nodes positions are relatives, but you can use methods of Camera class for translate the positions.
// Get relative position from coordinate
panel.getCamera().transformPxToGu(120, 20)
// Get node coordinate from a relative position
panel.getCamera().transformGuToPx(0, 10, 0)