簡體   English   中英

PathIterator類型的currentSegment(float [])方法不適用於參數(Double [])

[英]The method currentSegment(float[]) in the type PathIterator is not applicable for the arguments (Double[])

我正在嘗試在Java程序中讀取Path2D.Double的Segments,Eclips不斷告訴我:

PathIterator類型的currentSegment(float [])方法不適用於參數(Double [])

但是在PathIterator接口中,有2種方法似乎使Eclipse(或我)感到困惑

/**
 * Returns the coordinates and type of the current path segment in
 * the iteration.
 * The return value is the path-segment type:
 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
 * A float array of length 6 must be passed in and can be used to
 * store the coordinates of the point(s).
 * Each point is stored as a pair of float x,y coordinates.
 * SEG_MOVETO and SEG_LINETO types returns one point,
 * SEG_QUADTO returns two points,
 * SEG_CUBICTO returns 3 points
 * and SEG_CLOSE does not return any points.
 * @param coords an array that holds the data returned from
 * this method
 * @return the path-segment type of the current path segment.
 * @see #SEG_MOVETO
 * @see #SEG_LINETO
 * @see #SEG_QUADTO
 * @see #SEG_CUBICTO
 * @see #SEG_CLOSE
 */
public int currentSegment(float[] coords);

/**
 * Returns the coordinates and type of the current path segment in
 * the iteration.
 * The return value is the path-segment type:
 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
 * A double array of length 6 must be passed in and can be used to
 * store the coordinates of the point(s).
 * Each point is stored as a pair of double x,y coordinates.
 * SEG_MOVETO and SEG_LINETO types returns one point,
 * SEG_QUADTO returns two points,
 * SEG_CUBICTO returns 3 points
 * and SEG_CLOSE does not return any points.
 * @param coords an array that holds the data returned from
 * this method
 * @return the path-segment type of the current path segment.
 * @see #SEG_MOVETO
 * @see #SEG_LINETO
 * @see #SEG_QUADTO
 * @see #SEG_CUBICTO
 * @see #SEG_CLOSE
 */
public int currentSegment(double[] coords);

我自己的代碼如下:

PathIterator it= ((Path2D.Double) p3d).getPathIterator(null, 2d);
  Double x, y;
  while (!it.isDone()) {
     Double coords[]= new Double[6];
     System.out.println("1");
     int art= it.currentSegment(coords);
     System.out.println("2");
     switch (art) {
     case PathIterator.SEG_MOVETO:
        movetoXY(coords[0], coords[1]);
        x= coords[0];
        y= coords[1];
        break;
     case PathIterator.SEG_LINETO:
        linetoXY(coords[0], coords[1]);
        break;
     case PathIterator.SEG_CLOSE:
        linetoXY(x, y);
        break;
     default:
        System.out.println("unbekanntes Segment " + art);
        break;
     }
     it.next();
  }

與線

     int art= it.currentSegment(coords);

標記為紅色,工具提示顯示:

PathIterator類型的currentSegment(float [])方法不適用於參數(Double [])

有一個帶float []的方法,另一個帶double []的方法,但同名,我在這里想念什么?

Double []和double []的類型不同,因此您需要轉換這些類型...

Java8 Streams可以用語法上非常清晰的方式做到這一點:

只有一件重要的事情:Double是一個對象,因此Double []可以容納一個空值...您需要檢查一下!

Double[] d = { 1.0, 2.0, 3.0, 4.0, 5.0, null };
double[] dArray = Arrays.stream(d).mapToDouble(x -> x == null ? 0.0 : x.doubleValue()).toArray();
System.out.println(Arrays.toString(dArray));

如果您100%確定Double []沒有空值,則只需執行以下操作:

double[] dArray = Arrays.stream(d).mapToDouble(Double::doubleValue).toArray();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM