I'm creating a user interface for a java swing program that should be able to allow the user to create a network of nodes, connected by edges. The user is allowed to label those nodes and edges.
The UI currently has a custom graphical component that extends JPanel
and it is meaningful on it's own, having working operations such as zoom and drag. I'm trying to use the GraphStream library (http://graphstream-project.org/) to turn this custom panel into a View
that supports GraphStream Graph
s. Since GraphStream comes with a DefaultView
class that is able to show basic graphs out of the box and extends JPanel
, I decided to modify my map component so that it extends DefaultView
. So my class definition for the background component is now
public class CustomPanel extends DefaultView implements ICustomListener
{
public CustomPanel(Viewer viewer, String identifier, GraphRenderer renderer)
{
super(viewer, identifier, renderer);
...
}
}
However, the component fails to display any graphs or drawing capabilities. This is how I proceeded to form a test graph
Graph graph = new GraphicGraph("embedded");
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
Node a = graph.addNode("A");
a.addAttribute("xy", 0, 0);
Node b = graph.addNode("B");
b.addAttribute("xy", 10, 0);
Node c = graph.addNode("C");
c.addAttribute("xy", 10, 10);
graph.addEdge("AB","A","B");
//This is where I assume the GraphStream magic should happen.
CustomPanel myPanel = new CustomPanel(viewer, "defaultView", Viewer.newGraphRenderer());
viewer.addView(myPanel);
myPanel.display((GraphicGraph)(graph), true);
I am able to display the same graph with
viewer.addDefaultView(true);
but it opens in a new window.
So what am I doing wrong or is it even possible to display graphs with GraphStream on custom components? If there is another library that could do this I would be thankful to know. I am aware of JUNG2 and Java2D but have not tried them yet. I could also live with a solution that opens an editor in another window, but I would still need my CustomPanel as the background.