代码之家  ›  专栏  ›  技术社区  ›  Stefano Berti

如何获取mxCell的坐标?

  •  1
  • Stefano Berti  · 技术社区  · 8 年前

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

    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;
    }
    

    我错过了什么?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Johannes    8 年前

    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();
    
    推荐文章