如何在JGraphX中将顶点置于前面

时间:2014-08-20 11:22:58

标签: vertex jgraphx

我正在使用JGraphX,并且我有一个带有3个端口的顶点,但我希望顶点位于前面,由于某种原因它没有将顶点带到前面,我可能会丢失什么?

    final int PORT_DIAMETER = 20;
    final int PORT_RADIUS = PORT_DIAMETER / 2;
        mxGeometry geo1 = new mxGeometry(0, 0.5, PORT_DIAMETER,
                    PORT_DIAMETER);
        // Because the origin is at upper left corner, need to translate to
        // position the center of port correctly
        geo1.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
        geo1.setRelative(true);
        geo1.setWidth(10);

    mxCell port1 = new mxCell(null, geo1,
                    "port;image=/Images/blue-right-arrow.png");
    port1.setVertex(true);

    mxGeometry geo2 = new mxGeometry(1.0, 0.5, PORT_DIAMETER,
                PORT_DIAMETER);
    geo2.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
    geo2.setRelative(true);

    mxCell port2 = new mxCell(null, geo2,
                    "port;image=/Images/blue-right-arrow.png");
    port2.setVertex(true);

    mxGeometry geo3 = new mxGeometry(0.5, 1, PORT_DIAMETER,
                PORT_DIAMETER);
    geo3.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
    geo3.setRelative(true);

    mxCell port3 = new mxCell(null, geo3,
                    "port;image=/Images/blue-up-arrow.png");
    port3.setVertex(true);

    graph.addCell(port1, this);        
    graph.addCell(port2, this);
    graph.addCell(port3, this);
    graph.cellsOrdered(new Object[]{port1,port2,port3}, true);
    graph.cellsOrdered(new Object[]{this}, false);

1 个答案:

答案 0 :(得分:0)

我不确定这是否可行:端口有一个父p1(在你的情况下有一些'这个')和p1有一个父亲,例如p2(或默认父级)。当您带到前面或后面的单元格时,您只需更改相同父级上单元格的索引即可。在您的情况下,如果更改端口的父级,由于相对几何(必要),您将丢失它们。

您可以在示例GraphEditor类中验证这一点:创建一个大矩形,然后右键单击矩形 - > shape-> ender组,然后创建第二个形状,然后右键单击shape->退出组。然后尝试将第二个形状带回:右键单击形状 - >返回。没有什么变化。

如果适合您,一种解决方案是在您的“#”之上创建另一个单元格。单元格(具有端口的单元格):

double w = this.getGeometry().getWidth();
double h = this.getGeometry().getHeight();
mxGeometry geo4 = new mxGeometry(0, 0, w, h);
geo4.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));    
String s = this.getStyle(); //set the same style
mxCell c = new mxCell(null, geo4, style);
c.setVertex(true);
graph.addCell(c, this);  //set this to be the patern

另一个解决方案是编辑你的blue-right-arrow.png(端口)并用你的父顶点的样式绘制它的一半,以便创建它们在后面的错觉。

希望它有所帮助。