JUNG:改变顶点颜色

时间:2013-09-19 11:49:58

标签: java jung

我使用JUNG库来显示一个由服务器和链接组成的网络。

所以我像这样初始化我的图,Server和Link是我项目中的类:

            Graph<Server, Link> g;

    g = new SparseMultigraph<Server, Link>();

现在我需要更改特定顶点的颜色.. 所以我使用下面的代码:

//设置一个新的顶点来绘制变换器

    Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {
        public Paint transform(Integer i) {
            return Color.GREEN;
        }
    };

    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);

我得到了java.lang.ClassCastException异常,如下所示!!

  Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: network.Server cannot be cast to java.lang.Integer
at GUI.GUI$9.transform(GUI.java:1)
at edu.uci.ics.jung.visualization.renderers.BasicVertexRenderer.paintShapeForVertex(BasicVertexRenderer.java:98)
at edu.uci.ics.jung.visualization.renderers.BasicVertexRenderer.paintIconForVertex(BasicVertexRenderer.java:74)
at edu.uci.ics.jung.visualization.renderers.BasicVertexRenderer.paintVertex(BasicVertexRenderer.java:37)
at edu.uci.ics.jung.visualization.renderers.BasicRenderer.renderVertex(BasicRenderer.java:70)
at edu.uci.ics.jung.visualization.renderers.BasicRenderer.render(BasicRenderer.java:55)
at edu.uci.ics.jung.visualization.BasicVisualizationServer.renderGraph(BasicVisualizationServer.java:367)
at edu.uci.ics.jung.visualization.BasicVisualizationServer.paintComponent(BasicVisualizationServer.java:321)
at javax.swing.JComponent.paint(Unknown Source)

我在这里更新图表的完整代码:

private static BasicVisualizationServer updateGraph(网络网络){

            Set<Server> servers = network.getServers();
    Set<Link> links = network.getLinks();
            Graph<Server, Link> g;
    // Graph<V, E> where V is the type of the vertices and E is the type of
    // the edges
    g = new SparseMultigraph<Server, Link>();
    // Add some vertices and edges
    for (Server server : servers) {
        g.addVertex(server);
    }

    int i = 0;
    for (Link link : links) {
        g.addEdge(link, link.getSource(), link.getDest(), EdgeType.DIRECTED);
        i++;
    }


    // Layout<V, E>, VisualizationViewer<V,E>
    Layout<Integer, String> layout = new CircleLayout(g);
    layout.setSize(new Dimension(300, 300));
    VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
            layout);
    vv.setPreferredSize(new Dimension(350, 350));
    // Show vertex and edge labels
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());

     // Setup up a new vertex to paint transformer...
    Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {
        public Paint transform(Integer i) {
            return Color.GREEN;
        }
    };

    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);

    // Create a graph mouse and add it to the visualization component
    DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
    gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
    vv.setGraphMouse(gm);


    rightPanel.setTopComponent(vv);

    //Detecting selection of vertices
    final PickedState<Integer> pickedState = vv.getPickedVertexState();

    // Attach the listener that will print when the vertices selection changes.
    pickedState.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
        Object subject = e.getItem();
            // The graph uses Integers for vertices.
            if (subject instanceof Integer) {
                Integer vertex = (Integer) subject;
                if (pickedState.isPicked(vertex)) {
                    System.out.println("Vertex " + vertex
                        + " is now selected");
                } else {
                    System.out.println("Vertex " + vertex
                        + " no longer selected");
                }
            }
        }
    });

    return vv;
}

1 个答案:

答案 0 :(得分:3)

您的图表顶点类型为Server,但您的Transformer期望Integer作为输入。您需要更改代码以使其匹配。