Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取mxCell的坐标?_Java_Graph_Jgrapht_Jgraphx_Jgraph - Fatal编程技术网

Java 如何获取mxCell的坐标?

Java 如何获取mxCell的坐标?,java,graph,jgrapht,jgraphx,jgraph,Java,Graph,Jgrapht,Jgraphx,Jgraph,我需要得到一个mxCell的坐标(x,y),我通过他的Id找到它,但是当我调用它的getGeometry()时,它会给我null,在我得到NullPointerException之后 private double getX(String node){ mxCell cell = (mxCell) ((mxGraphModel)map.getGraph().getModel()).getCell(node); mxGeometry geo = cell.getGeometry();

我需要得到一个mxCell的坐标(x,y),我通过他的Id找到它,但是当我调用它的getGeometry()时,它会给我null,在我得到NullPointerException之后

private double getX(String node){
    mxCell cell = (mxCell) ((mxGraphModel)map.getGraph().getModel()).getCell(node);
    mxGeometry geo = cell.getGeometry();//this line give me the null value
    double x = geo.getX();//NullPointerException
    return x;
}
map是包含所有图形的mxGraphComponent


我缺少什么?

我假设您的
字符串节点
参数应该映射到单元格的
id

基本上,您可以选择所有单元格,获取它们并对其进行迭代。由于JGraph中几乎所有内容都是
对象
,因此需要进行一些强制转换

private double getXForCell(String id) {
    double res = -1;
    graph.clearSelection();
    graph.selectAll();
    Object[] cells = graph.getSelectionCells();
    for (Object object : cells) {
        mxCell cell = (mxCell) object;
        if (id.equals(cell.getId())) {
            res = cell.getGeometry().getX();
        }
    }
    graph.clearSelection();
    return res;
}
在调用
getGeometry()
之前,您最好先检查cell.isVertex(),因为它在边缘上的实现方式不同

编辑:遵循你的方法,下面的内容对我也很有用。似乎您需要额外的cast
(mxCell)


我还发现了
void graph.selectVertices()
方法,如果只想查询顶点,该方法可能很有用。它会导致一些问题,因为它必须由EDT完成,因为选择也是图形化的
mxGraphModel graphModel = (mxGraphModel) graph.getModel();
return ((mxCell) graphModel.getCell(id)).getGeometry().getX();