2014-02-12 1 views
1

JGraphx를 통해 순서도를 표시하려고하는데 입출력 블록에 평행 사변형이 필요합니다. 그러나 JGraphx는 "shape = parallelogram"을 알지 못합니다. 그래프와 플로우 차트 용 라이브러리에 평행 사변형을 두지 않는 것이 이상하게 보입니다 (그리고 "평행 사변형"이없는 방법은 "액터"모양도 있습니다). 어쩌면 단지 다른 이름으로 지어 졌을 까? 또는 실제로 미리 정의 된 평행 사변형 모양이없는 경우 꼭지점을 평행 사변형으로 만들려면 어떻게해야합니까?JGraphx에서 평행 사변형 모양의 꼭지점을 만드는 방법은 무엇입니까?

답변

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()); 

이제 "모양 = 평행 사변형"이 올바르게 작동합니다.

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

완성을 위해 : 평행 평행 사변형은 미리 정의 됨 : SHAPE_RHOMBUS입니다.

관련 문제