繁体   English   中英

使用JGraph添加顶点,边和端口

[英]Adding vertices, edges and ports using JGraph

我想添加新的顶点和边,并使用JGraph库创建一个图形。 我总是收到此java.lang.NullPointer异常。 我创建了一个类函数createvertex,以创建新的单元格/顶点并绘制连接端口的边缘。 但是即使我尚未将端口声明为null,该端口也始终显示为null。 下面是我的代码。 我的代码有什么问题吗?

public class HelloWorld {

public static void main(String[] args) {

    // Construct Model and Graph
    GraphModel model = new DefaultGraphModel();
    GraphLayoutCache view= new GraphLayoutCache(model,new DefaultCellViewFactory());

    JGraph graph = new JGraph(model,view);

    // Control-drag should clone selection
    graph.setCloneable(true);

    // Enable edit without final RETURN keystroke
    graph.setInvokesStopCellEditing(true);

    // When over a cell, jump to its default port (we only have one, anyway)
    graph.setJumpToDefaultPort(true);

    // Insert all three cells in one call, so we need an array to store them
    DefaultGraphCell[] cells = new DefaultGraphCell[5];
    DefaultPort[] port = new DefaultPort[4];

    // Create Hello Vertex
    cells[0] = createVertex("Hello", 20, 20, 40, 20, Color.BLACK, true);

    port[0].setParent(cells[0]);

    // Create World Vertex
    cells[1] = createVertex("World", 140, 140, 40, 20, Color.ORANGE, true);
    cells[1].add(port[1]);
    cells[1].add(port[2]);
    port[1].setParent(cells[1]);
    port[2].setParent(cells[1]);

    cells[3]=  createVertex("Optical Cards",150,150,20,40,Color.GREEN, true);
    cells[3].add(port[3]);
    port[3].setParent(cells[3]);


    // Create Edge
    DefaultEdge[] edge = new DefaultEdge[2];

    // Fetch the ports from the new vertices, and connect them with the edge
    edge[0].setSource(cells[0].getChildAt(0));
    edge[0].setTarget(cells[1].getChildAt(0));

    cells[2] = edge[0];

    edge[1].setSource(cells[1].getChildAt(1));
    edge[1].setTarget(cells[3].getChildAt(0));

    cells[4]=edge[1];

    // Set Arrow Style for edge
    int arrow = GraphConstants.ARROW_CLASSIC;
    GraphConstants.setLineEnd(edge[0].getAttributes(), arrow);
    GraphConstants.setEndFill(edge[0].getAttributes(), true);

    GraphConstants.setLineEnd(edge[1].getAttributes(), arrow);
    GraphConstants.setEndFill(edge[1].getAttributes(), true);

    // Insert the cells via the cache, so they get selected
    graph.getGraphLayoutCache().insert(cells);

    // Show in Frame
    JFrame frame = new JFrame();
    frame.getContentPane().add(new JScrollPane(graph));
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static DefaultGraphCell createVertex(String name, double x,double y,double w,double h, Color bg, boolean raised) {

    // Create vertex with the given name
    DefaultGraphCell cell = new DefaultGraphCell(name);

    // Set bounds
    GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(
            x, y, w, h));

    // Set fill color

        GraphConstants.setGradientColor(cell.getAttributes(), Color.orange);
        GraphConstants.setOpaque(cell.getAttributes(), true);

    // Set raised border
    if (raised)
        GraphConstants.setBorder(cell.getAttributes(), BorderFactory
                .createRaisedBevelBorder());
    else
        // Set black border
        GraphConstants.setBorderColor(cell.getAttributes(), Color.black);

    // Add a Port
    DefaultPort port = new DefaultPort();
    cell.add(port);

    return cell;
}
}
DefaultPort[] port = new DefaultPort[4];




// Create Hello Vertex
cells[0] = createVertex("Hello", 20, 20, 40, 20, Color.BLACK, true);

port[0].setParent(cells[0]);  //   <---------- port[0] is null here, so you 
                              //               cannot call setParent()

仅仅因为初始化端口数组并不意味着您可以简单地使用数组中的第一个DefaultPort元素。 在尝试取消引用数组中的项目之前,需要使用有效的非null DefaultPort对象填充数组。

暂无
暂无

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

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