0

我正在使用 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);
4

1 回答 1

0

I am not sure whether this is possible: the ports have a parent p1 (in your case some 'this') and p1 has a parent e.g. p2 (or the default parent). when you bring to front or back a cell you just alter the index of the cell on the same parent. In your case, if you change the parent of the ports, you will loose them because of the relative geometry (that is necessary).

You can verify this in the example GraphEditor class: create a large rectangle, then right click on the rectangle->shape->ender group, then create a second shape, then right click shape->exit group. Then try to bring to back the second shape: right click on to the shape shape->to back. Nothing changes.

One solution, if it suits you, is to create another cell on top of your 'this' cell (the one with the ports):

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

Another solution is to edit your blue-right-arrow.png (port) and paint the half of it with the style of your parent vertex, in order to create the illusion that they are at the back.

Hope it helps.

于 2014-09-10T16:35:07.453 回答