JGraph(T)图元选择定制

发布于 2025-01-08 03:23:33 字数 1457 浏览 0 评论 0原文

我在使用 JGraph(T) 库时遇到一些问题。 我需要更改默认选择视图,例如:默认背景为橙色,如果选择顶点则添加绿色边框,我可以更改此可视化策略以将所选元素上的背景更改为 Color.BLUE。 我尝试执行以下代码:

 GraphSelectionModel graphSelectionModel = new DefaultGraphSelectionModel(jGraph);
    graphSelectionModel.setSelectionMode(GraphSelectionModel.MULTIPLE_GRAPH_SELECTION);
    graphSelectionModel.addGraphSelectionListener(new GraphSelectionListener()
    {
        HashMap oldestCellsAndAttrs = new HashMap();
        @Override
        public void valueChanged(GraphSelectionEvent e)
        {
            jGraph.getModel().beginUpdate();
            m_jgAdapter.edit(oldestCellsAndAttrs, null, null, null);
            oldestCellsAndAttrs.clear();
            Map cellAndAttrs = new HashMap();
            for (Object obj : e.getCells())
            {
                DefaultGraphCell cell = (DefaultGraphCell) obj;
                oldestCellsAndAttrs.put(cell, JGraphModelAdapter.createDefaultVertexAttributes());
                Map attrs = cell.getAttributes();
                GraphConstants.setBackground(attrs, Color.BLUE);
                cellAndAttrs.put(cell, attrs);
            }
            m_jgAdapter.edit(cellAndAttrs, null, null, null);
            jGraph.getModel().endUpdate();
        }
    });
    fillGraph(tree, g);
    layout(g, m_jgAdapter, jGraph);
    setSize(3 * width / 4, height);
    jGraph.setSelectionModel(graphSelectionModel);

这会在相同的选定对象上更改 bkg,但在未选择后不会返回。 是否存在默认解决此问题的方法?

I have as some problem with using JGraph(T) library.
I need change default selection view, example: default background is orange color, if vertex selected then green border was added, can i change this visualization strategy to change background to Color.BLUE on selected element.
I try execute follow code:

 GraphSelectionModel graphSelectionModel = new DefaultGraphSelectionModel(jGraph);
    graphSelectionModel.setSelectionMode(GraphSelectionModel.MULTIPLE_GRAPH_SELECTION);
    graphSelectionModel.addGraphSelectionListener(new GraphSelectionListener()
    {
        HashMap oldestCellsAndAttrs = new HashMap();
        @Override
        public void valueChanged(GraphSelectionEvent e)
        {
            jGraph.getModel().beginUpdate();
            m_jgAdapter.edit(oldestCellsAndAttrs, null, null, null);
            oldestCellsAndAttrs.clear();
            Map cellAndAttrs = new HashMap();
            for (Object obj : e.getCells())
            {
                DefaultGraphCell cell = (DefaultGraphCell) obj;
                oldestCellsAndAttrs.put(cell, JGraphModelAdapter.createDefaultVertexAttributes());
                Map attrs = cell.getAttributes();
                GraphConstants.setBackground(attrs, Color.BLUE);
                cellAndAttrs.put(cell, attrs);
            }
            m_jgAdapter.edit(cellAndAttrs, null, null, null);
            jGraph.getModel().endUpdate();
        }
    });
    fillGraph(tree, g);
    layout(g, m_jgAdapter, jGraph);
    setSize(3 * width / 4, height);
    jGraph.setSelectionModel(graphSelectionModel);

This change bkg on same selected objs, but not return after non-selection.
Is exists default solve for this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-01-15 03:23:33

我用以下代码解决了问题:

            @Override
        public void valueChanged(GraphSelectionEvent e)
        {
           Object[] cells = e.getCells();
            HashMap<DefaultGraphCell, AttributeMap> cellsAndAttrs = new HashMap<DefaultGraphCell, AttributeMap>();
            for (Object c : cells)
            {
                DefaultGraphCell cell = (DefaultGraphCell) c;
                AttributeMap cellAttrs = cell.getAttributes();
                if (jGraph.isCellSelected(cell))
                    GraphConstants.setBackground(cellAttrs, SELECTED_COLOR);
                else
                    GraphConstants.setBackground(cellAttrs, NON_SELECTED_COLOR);
                cellsAndAttrs.put(cell, cellAttrs);
            }
            m_jgAdapter.edit(cellsAndAttrs, null, null, null);
        }

I solve problem with foollowing code:

            @Override
        public void valueChanged(GraphSelectionEvent e)
        {
           Object[] cells = e.getCells();
            HashMap<DefaultGraphCell, AttributeMap> cellsAndAttrs = new HashMap<DefaultGraphCell, AttributeMap>();
            for (Object c : cells)
            {
                DefaultGraphCell cell = (DefaultGraphCell) c;
                AttributeMap cellAttrs = cell.getAttributes();
                if (jGraph.isCellSelected(cell))
                    GraphConstants.setBackground(cellAttrs, SELECTED_COLOR);
                else
                    GraphConstants.setBackground(cellAttrs, NON_SELECTED_COLOR);
                cellsAndAttrs.put(cell, cellAttrs);
            }
            m_jgAdapter.edit(cellsAndAttrs, null, null, null);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文