2

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

所以我像这样初始化我的图表,服务器和链接是我项目中的类:

            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)

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

私有静态 BasicVisualizationServer updateGraph(Network network) {

            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;
}
4

1 回答 1

3

您的图形顶点类型是Server,但您Transformer期望 aInteger作为输入。您需要更改代码以使它们匹配。

于 2013-09-19T12:17:48.840 回答