如何在JGraphx中制作平行四边形的顶点?

时间:2014-02-12 07:03:54

标签: java swing shapes jgraphx

我正在尝试通过JGraphx显示流程图,我需要一个输入/输出块的平行四边形。但是JGraphx似乎并不知道“shape = parallelogram”。在图表和流程图的库中没有平行四边形似乎很奇怪(它甚至具有“演员”形状,为什么它没有平行四边形?)。也许它只是以其他方式命名?或者在确实没有预定义的平行四边形形状的情况下,我该怎么做才能使顶点成为平行四边形?

2 个答案:

答案 0 :(得分:3)

最后,我找到了制作平行四边形的方法,耶!这就是我如何使它发挥作用 首先,要创建自定义形状,我必须创建自己的类,扩展mxBasicShape并覆盖createShape方法。

public class Parallelogram extends mxBasicShape {
public Shape createShape(mxGraphics2DCanvas canvas, mxCellState state){
    mxCell cell = (mxCell)state.getCell();
    Polygon polygon = new Polygon();
    if(cell != null && cell.getGeometry() != null) {
        mxGeometry g = cell.getGeometry();
        int dx = (int) (cell.getGeometry().getHeight()/4.0);
        polygon.addPoint((int)(g.getX()+dx), (int)g.getY());
        polygon.addPoint((int)(g.getX()+g.getWidth()+dx), (int)g.getY());
        polygon.addPoint((int)(g.getX()+g.getWidth()-dx), (int)(g.getY()+g.getHeight()));
        polygon.addPoint((int)((int)g.getX()-dx), (int)(g.getY()+g.getHeight()));
    }
    return polygon;

}

}

第二步是将其添加到似乎存储在mxGraphics2DCanvas中的形状列表中。

mxGraphics2DCanvas.putShape("parallelogram", new Parallelogram());

现在“shape = parallelogram”工作正常!

<强> UPD
它似乎只是创造一个形状是不够的,还要创建一个周长。这就是我做到的:

public class ParallelogramPerimeter implements mxPerimeterFunction {
    @Override
    public mxPoint apply(mxRectangle bounds, mxCellState vertex, mxPoint next,
            boolean orthogonal) {
        double cx = bounds.getCenterX();
        double cy = bounds.getCenterY();
        double nx = next.getX();
        double ny = next.getY();
        double pi = Math.PI;
        double pi2 = Math.PI/2.0;
        double h = bounds.getHeight();
        double w = bounds.getWidth();
        double alpha = Math.atan2(h/2.0, w/2.0+h/4.0);
        double beta = Math.atan2(h/2.0, w/2.0-h/4.0);
        double t = Math.atan2(ny-cy, nx-cx);

        mxPoint p = new mxPoint();

        //Left
        if (t > pi - alpha || t < (-pi)+beta){
            Line border = new Line(cx-w/2.0+h/4.0, cy-h/2.0, cx-w/2.0-h/4.0, cy+h/2.0);
            Line line = new Line(cx, cy, nx, ny);
            p = Line.intersection(border, line);
        }
        //Top
        else if (t > (-pi)+beta && t < (0-alpha)){
            p.setY(cy-h/2.0);
            p.setX(cx + h/2.0*Math.tan(pi2+t));
        }
        //Right
        else if (t > (0-alpha) && t < beta){
            Line border = new Line(cx+w/2.0+h/4.0, cy-h/2.0, cx+w/2.0-h/4.0, cy+h/2.0);
            Line line = new Line(cx, cy, nx, ny);
            p = Line.intersection(border, line);
        }
        //Bottom
        else {
            p.setY(cy+h/2.0);
            p.setX(cx + h/2.0*Math.tan(pi2-t));
        }

        if (orthogonal){
            //Top
            if (nx >= cx-w/2.0+h/4.0 && nx <= cx+w/2.0+h/4.0 && ny <= cy-h/2.0){
                p.setX(nx);
            }
            //Bottom
            else if (nx >= cx - w/2.0-h/4.0 && nx <= cx+w/2.0-h/4.0 && ny >= cy+h/2.0){
                p.setX(nx);
            }
            //Left or right
            else{
                Line left = new Line(cx-w/2.0+h/4.0, cy-h/2.0, cx-w/2.0-h/4.0, cy+h/2.0);
                Line right = new Line(cx+w/2.0+h/4.0, cy-h/2.0, cx+w/2.0-h/4.0, cy+h/2.0);
                p = left.projection(nx, ny);
                mxPoint p1 = right.projection(nx, ny);
                boolean r = false;
                if (distance(nx, ny, p.getX(), p.getY()) > distance(nx, ny, p1.getX(), p1.getY()))
                    {
                        p = p1;
                        r = true;
                    }
                //Upper corners
                if (p.getY() < cy-h/2.0){
                    p.setY(cy-h/2.0);
                    if(r){
                        p.setX(cx+w/2.0+h/4.0);
                    }
                    else
                    {
                        p.setX(cx-w/2.0+h/4.0);
                    }
                }
                //Lower corners
                else if (p.getY() > cy+h/2.0){
                    p.setY(cy+h/2.0);
                    if(r){
                        p.setX(cx+w/2.0-h/4.0);
                    }
                    else
                    {
                        p.setX(cx-w/2.0-h/4.0);
                    }
                }
            }
        }

        return p;

    }

    private double distance(double x1, double y1, double x2, double y2){
        return Math.sqrt(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2));
    }

}

class Line{
private double a;
private double b;
private double c;

Line(double x1, double y1, double x2, double y2){
    a = y1-y2;
    b = x2-x1;
    c = x1*y2-x2*y1;
}

private Line(double a, double b, double c){
    this.a = a;
    this.b = b;
    this.c = c;
}

static private double determinant(double i, double j, double k, double l){
    return i*l - k*j;
}

static mxPoint intersection(Line first, Line second){
    double x,y;
    double denominator = determinant(first.a, first.b, second.a, second.b);
    x = 0 - determinant(first.c, first.b, second.c, second.b)/denominator;
    y = 0 - determinant(first.a, first.c, second.a, second.c)/denominator;
    return new mxPoint(x,y);
}

mxPoint projection(double x, double y){
    double a,b,c;
    if (this.b!=0){
        a=1;
        b=-(this.a*a)/this.b;
    }
    else{
        b = 1;
        a=-(this.b*b)/this.a;
    }
    c = -(a*x+b*y);
    Line line = new Line(a,b,c);
    return intersection(this, line);
}
}

然后我必须将它添加到使用的周界,其列表似乎与形状不在同一个位置,但在mxStyleRegistry中:

mxStyleRegistry.putValue("parallelogramPerimeter", new ParallelogramPerimeter());

最后我使用了“shape = parallelogram; perimeter = parallelogramPerimeter”作为样式,它现在不仅用于显示平行四边形,还用于正确连接边缘。

答案 1 :(得分:1)

为了完整性:等边平行四边形是预定义的:SHAPE_RHOMBUS