繁体   English   中英

如何在JGraphX lib中删除Vertex?

[英]How to remove Vertex in JGraphX lib?

我正在尝试解决一个需要在Swing中显示图形的任务。 找到了JGraph框架(现在称为JGraphX)。 在示例中, graph.insertVertex(...)方法用于添加顶点,而graph.insertEdge(...)方法用于graph.insertEdge(...)好的,它工作正常。

但是之后我该如何删除该顶点?

看起来您可以使用mxGraph.removeCells方法删除顶点。 我已经修改了JGraphX中包含的HelloWorld示例(使用3.4.1版 ):

import com.mxgraph.swing.*;
import com.mxgraph.view.*;
import javax.swing.*;

/**
 * Adapted from https://github.com/jgraph/jgraphx/blob/master/examples
 *              /com/mxgraph/examples/swing/HelloWorld.java
 */
public class GoodbyeVertex extends JFrame {
    private static final long serialVersionUID = -2707712944901661771L;

    public static void main(String[] args) {
        GoodbyeVertex frame = new GoodbyeVertex();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
    }

    public GoodbyeVertex() {
        super("Hello, World!");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 30);
            Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30);

            graph.insertEdge(parent, null, "Edge", v1, v2);

            // Remove a vertex. The related edge is removed as well.
            graph.removeCells(new Object[]{v1});
        } finally {
            graph.getModel().endUpdate();
        }

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM