2017-12-09 17 views
0

Path2Dオブジェクトの各座標セットの座標を取得する必要がありますが、私はどのようにわかりません。以前はPolygonsを使用していたので、長さがPolygon.npointsの2つの配列を初期化してから、Polygon.xpointsPolygon.ypoints配列に設定することができました。 Path2Dオブジェクトを使用しているので、私はこれを行う方法がわかりません。なぜなら、入力として配列をとり、セグメントを返すPathIteratorを初期化するだけだからです。誰かがPath2Dオブジェクトのすべての座標ペアを取得する方法を説明できますか?以下はJavaでPath2Dオブジェクトの座標ペアを取得しますか?

答えて

2

あなたはすべてのセグメントを取得し、 PathIteratorのペアを調整できる方法の例です:

あなたが繰り返しPathIteratorcurrentSegmentメソッドを呼び出します。 呼び出しごとに、1つのセグメントの座標が得られます。 特に、座標数はセグメントタイプ (currentSegmentメソッドから取得した戻り値)によって異なります。

public static void dump(Shape shape) { 
    float[] coords = new float[6]; 
    PathIterator pathIterator = shape.getPathIterator(new AffineTransform()); 
    while (!pathIterator.isDone()) { 
     switch (pathIterator.currentSegment(coords)) { 
     case PathIterator.SEG_MOVETO: 
      System.out.printf("move to x1=%f, y1=%f\n", 
        coords[0], coords[1]); 
      break; 
     case PathIterator.SEG_LINETO: 
      System.out.printf("line to x1=%f, y1=%f\n", 
        coords[0], coords[1]); 
      break; 
     case PathIterator.SEG_QUADTO: 
      System.out.printf("quad to x1=%f, y1=%f, x2=%f, y2=%f\n", 
        coords[0], coords[1], coords[2], coords[3]); 
      break; 
     case PathIterator.SEG_CUBICTO: 
      System.out.printf("cubic to x1=%f, y1=%f, x2=%f, y2=%f, x3=%f, y3=%f\n", 
        coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); 
      break; 
     case PathIterator.SEG_CLOSE: 
      System.out.printf("close\n"); 
      break; 
     } 
     pathIterator.next(); 
    }  
} 

あなたはどんなShape をダンプするために、この方法を使用することができます(Rectangleのようなその実装、PolygonEllipse2DPath2Dのためにも、したがってと、...)

Shape shape = ...; 
dump(shape); 
関連する問題