我在GraphStream中有一个项目,在这个项目中,我需要修改一个图的节点坐标。
这些坐标存储在一个名为"xy"的变量中。
完成这项工作的函数应该是setAttribute()或addAttribute(),但是当我使用它们时,什么都不会发生,或者有时它会给我一条NaN消息。
这是代码的一个示例,它根本不做任何更改:
public class TestClass {
public static void main(String[] args) {
Graph graph = new MultiGraph("Dorogovtsev mendes");
Generator gen = new DMCoordGen(); // Coordinates Generator
gen.addSink(graph);
gen.begin();
for (int j = 0; j < 5; j++) { // Graph Nodes
gen.nextEvents();
}
gen.end();
System.out.println("-----------------");
System.out.println("First Coordinates");
System.out.println("-----------------");
for(int i=0; i<graph.getNodeCount(); i++) { // First Reading
Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
Double x = attributes[0];
Double y = attributes[1];
System.out.println(x + " , " + y);
}
System.out.println("---------------");
System.out.println("New Coordinates");
System.out.println("---------------");
for(int i=0; i<graph.getNodeCount(); i++) { // Modification
Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
Double x = attributes[0] * 100;
Double y = attributes[1] * 100;
graph.getNode(i).setAttribute("xy", (Object[]) attributes);
}
for(int i=0; i<graph.getNodeCount(); i++) { // Second Reading
Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
Double x = attributes[0];
Double y = attributes[1];
System.out.println(x + " , " + y);
}
}
}此代码返回的结果是:
-----------------
First Coordinates
-----------------
0.27463410536937105 , 0.908142618579691
0.5945324304252239 , 0.011861230502362319
0.7645069243611142 , 0.8994092027470882
0.23856199477010953 , 0.6174255664753833
0.9215549969974312 , 0.46748048612026005
0.5283548936726747 , 0.3995089175747245
0.14035732608566487 , 0.32181008971710223
0.8782155705197804 , 0.8271792979519879
---------------
New Coordinates
---------------
0.27463410536937105 , 0.908142618579691
0.5945324304252239 , 0.011861230502362319
0.7645069243611142 , 0.8994092027470882
0.23856199477010953 , 0.6174255664753833
0.9215549969974312 , 0.46748048612026005
0.5283548936726747 , 0.3995089175747245
0.14035732608566487 , 0.32181008971710223
0.8782155705197804 , 0.8271792979519879如您所见,使用setAttribute()或addAttribute()都没有修改任何内容。
请参考graph.getNode(i).setAttribute("xy", (Object[]) attributes);行。
我做错什么了?我怎么才能修好它呢?
谢谢!
发布于 2015-11-24 11:22:42
在您的例子中,您是否有意:
attributes[0] *= 100;而不是:
Double x = attributes[0] * 100;在您的示例中,attributes数组从未被修改过。这可能就是为什么“setAttribute”呼叫(显然)没有效果的原因。
https://stackoverflow.com/questions/33891193
复制相似问题