3

我使用 JUNG(Java 通用网络/图形框架)和以下代码构建了一个图形:

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);

现在,我想在图中添加更多顶点和边。我该怎么做?我应该运行什么指令来重绘图表?谢谢!

4

3 回答 3

5

将边和顶点添加到图形后,您必须调用vv.repaint()以绘制更改。

于 2012-01-19T12:48:59.527 回答
1

如果要添加顶点和边:

//add some Vertex and Edges
g.addVertex((BusStop)obj1);
g.addVertex((BusStop)obj2);
g.addEdge((Travel) trv1, obj1, obj2);

例如查看在SimpleGraphView.java中如何使用addVertexaddEdge

于 2011-05-22T22:50:33.280 回答
1

如果您要在用户交互后重新绘制图形,则必须将EditingModalGraphMouse添加到VisualizationViewer

    EditingModalGraphMouse gm = new EditingModalGraphMouse(vv.getRenderContext(), 
             vertexFactory, edgeFactory); 
    vv.setGraphMouse(gm);

构造函数必须使用派生自的vertexFactoryedgeFactory对象

Factory<E> and Factory<V>

其工作是通过create()方法创建边缘/顶点类的新实例

Factory <BusStop> vertexFactory = new Factory<BusStop>() {
            public BusStop create() {
                return new BusStop();
            }
        };

edgeFactory 也一样

于 2011-05-30T21:55:11.213 回答