[英]setAttribute in GraphStream
我在GraphStream
中有一个项目, 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);
。
我做错了什么 ? 我该如何解决?
谢谢 !
在您的示例中,您的意思是:
attributes[0] *= 100;
代替 :
Double x = attributes[0] * 100;
在您的情况下, attributes
数组永远不会被修改。 这可能就是为什么“ setAttribute”调用(显然)无效的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.