我使用JUNG(Java Universal Network / Graph Framework)使用以下代码构建图形:
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);
现在,我想为图表添加更多顶点和边缘..我该怎么做?我应该运行哪些指令来重绘图形?谢谢!
答案 0 :(得分:5)
在图表中添加边和顶点后,您必须调用vv.repaint()
来绘制更改。
答案 1 :(得分:1)
如果要添加顶点和边:
//add some Vertex and Edges
g.addVertex((BusStop)obj1);
g.addVertex((BusStop)obj2);
g.addEdge((Travel) trv1, obj1, obj2);
例如,请参阅SimpleGraphView.java
中如何使用 addVertex 和 addEdge答案 2 :(得分:1)
如果您要在用户交互后重新绘制图表,则必须将 EditingModalGraphMouse 添加到 VisualizationViewer
EditingModalGraphMouse gm = new EditingModalGraphMouse(vv.getRenderContext(),
vertexFactory, edgeFactory);
vv.setGraphMouse(gm);
构造函数必须使用 vertexFactory 和 edgeFactory 对象派生
Factory<E> and Factory<V>
其工作是通过 create()方法创建一个新的edge / vertices类实例
Factory <BusStop> vertexFactory = new Factory<BusStop>() {
public BusStop create() {
return new BusStop();
}
};
与 edgeFactory
相同