java - JGraph(T)图元选择定制

标签 java jgrapht

我在使用 JGraph(T) 库时遇到一些问题。 我需要更改默认选择 View ,例如:默认背景为橙色,如果选择顶点然后添加绿色边框,我可以更改此可视化策略以将所选元素上的背景更改为 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,但在未选择后不会返回。 是否存在默认解决此问题的方法?

最佳答案

我用愚蠢的代码解决了问题:

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

关于java - JGraph(T)图元选择定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9392278/

相关文章:

java - 添加两条边 - JGraphT

java - 是否可以使用 jGraphT 检查 TicTacToe 游戏的获胜条件?

java - 如何在启动屏幕上显示 jbutton

JavaFX 如何在不损失质量的情况下使用和缩放图像/图标?

java - 字符串无法转换为 T

Java 反射

java - Java 进程使用的总内存和堆大小

java - 在 jgrapht 中修剪有向无环图的更有效方法?

java - 在有向图中找到具有权重限制的两个顶点之间的所有路径