首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setAttribute in GraphStream

setAttribute in GraphStream
EN

Stack Overflow用户
提问于 2015-11-24 10:28:56
回答 1查看 484关注 0票数 0

我在GraphStream中有一个项目,在这个项目中,我需要修改一个图的节点坐标。

这些坐标存储在一个名为"xy"的变量中。

完成这项工作的函数应该是setAttribute()addAttribute(),但是当我使用它们时,什么都不会发生,或者有时它会给我一条NaN消息。

这是代码的一个示例,它根本不做任何更改:

代码语言:javascript
复制
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);
    }
}

}

此代码返回的结果是:

代码语言:javascript
复制
-----------------
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);行。

我做错什么了?我怎么才能修好它呢?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-24 11:22:42

在您的例子中,您是否有意:

代码语言:javascript
复制
attributes[0] *= 100;

而不是:

代码语言:javascript
复制
Double x = attributes[0] * 100;

在您的示例中,attributes数组从未被修改过。这可能就是为什么“setAttribute”呼叫(显然)没有效果的原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33891193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档