2017-12-09 1 views
0

Ich muss die Koordinaten von jedem Satz von Koordinaten in einem Path2D-Objekt erhalten, aber ich weiß nicht wie. Zuvor verwendeten wir Polygone. Daher konnte ich zwei Arrays der Länge Polygon.npoints initialisieren und sie dann als Polygon.xpoints und Polygon.ypoints Arrays festlegen. Nun, da wir Path2D-Objekte verwenden, habe ich keine Ahnung, wie das geht, weil ich scheinbar nur einen PathIterator initialisiere, der ein Array als Eingabe nimmt und Segmente zurückgibt? Kann jemand erklären, wie man alle Koordinatenpaare eines Path2D-Objekts erhält?Die Koordinatenpaare eines Path2D-Objekts in Java abrufen?

Antwort

2

Unten ist ein Beispiel, wie Sie alle Segmente erhalten und Koordinatenpaare eines PathIterator:

Sie die PathIterator nennen ‚s currentSegment Verfahren wiederholt. Bei jedem Aufruf erhalten Sie die Koordinaten eines Segments. Beachten Sie insbesondere, dass die Anzahl der Koordinaten vom Segmenttyp abhängt (der Rückgabewert, den Sie von der Methode currentSegment erhalten haben).

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

Sie können diese Methode für jede Shape Dumping (und damit auch für seine Implementierungen wie Rectangle, Polygon, Ellipse2D, Path2D, ...)

Shape shape = ...; 
dump(shape); 
Verwandte Themen