Redraw Graph on JUNG
I build a graph using JUNG (Java Universal Network/Graph Framework) with the following code:
g = new SparseMultigraph<BusStop, Travel>();
//add some Vertex and Edges
Layout<String, String> layout1 = new CircleLayout(g);
layout1.setSize(new Dimension(300,300)); // sets the initial size of the layout space
VisualizationViewer vv = new VisualizationViewer(layout1);
vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size
Transformer<BusStop,Paint> vertexPaint = new Transformer<BusStop,Paint>() {
public Paint transform(BusStop b) {
return Color.GREEN;
}
};
Transformer<BusStop,Shape> vertexShape = new Transformer<BusStop,Shape>() {
public Shape transform(BusStop b) {
return new Rectangle(-20, -10, 40, 20);
}
};
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
vv.getRenderContext().setVertexShapeTransformer(vertexShape);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
GraphViewerForm = new edu.uci.ics.jung.visualization.GraphZoomScrollPane(vv);
Now, I wa开发者_如何学Cnt to add more vertices and edges to the graph.. how can I do this? What instructions should I run for the graph to be redrawn? Thanks!
After adding edges and vertices to the graph, you must call vv.repaint()
to draw the changes.
If you are looking for redrawing the graph after user interaction, you have to add an EditingModalGraphMouse to your VisualizationViewer
EditingModalGraphMouse gm = new EditingModalGraphMouse(vv.getRenderContext(),
vertexFactory, edgeFactory);
vv.setGraphMouse(gm);
the constructor must be fed with vertexFactory and edgeFactory objects derived from
Factory<E> and Factory<V>
whose job is to create a new instance of edge/vertices class via the create() method
Factory <BusStop> vertexFactory = new Factory<BusStop>() {
public BusStop create() {
return new BusStop();
}
};
same for the edgeFactory
If you want to add vertices and edges:
//add some Vertex and Edges
g.addVertex((BusStop)obj1);
g.addVertex((BusStop)obj2);
g.addEdge((Travel) trv1, obj1, obj2);
For example see how addVertex and addEdge is being used in SimpleGraphView.java
精彩评论