我需要获得一个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。
我遗漏了什么?
发布于 2017-07-06 04:38:16
我假设您的String node
参数应该映射到单元格的id
。
基本上,您选择所有的单元格,获取它们并对它们进行迭代。由于JGraph中的几乎所有东西都是Object
,所以需要进行一些转换。
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;
}
您最好在调用cell.isVertex()
之前检查它是否是getGeometry()
,因为它在边缘上的实现方式不同。
编辑:遵循你的方法,下面的作品也适用于我。似乎你需要额外的演员(mxCell)
。
mxGraphModel graphModel = (mxGraphModel) graph.getModel();
return ((mxCell) graphModel.getCell(id)).getGeometry().getX();
https://stackoverflow.com/questions/44823886
复制