gpt4 book ai didi

java - 荣格 : Changing the vertex colour

转载 作者:行者123 更新时间:2023-11-30 08:30:04 31 4
gpt4 key购买 nike

我使用 JUNG 库通过图形可视化了一个由服务器和链接组成的网络。

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

            Graph<Server, Link> g;

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

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

//设置一个新的顶点来绘制transformer

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

最佳答案

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

关于java - 荣格 : Changing the vertex colour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18893838/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com