簡體   English   中英

如何使用Java繪制不完整的多邊形

[英]How to draw an incomplete Polygon using Java

我想要做的是使用Java繪制不完整的多邊形。 我已經弄清楚了如何一次繪制僅繪制多邊形,甚至填充多邊形。 我也可以通過使用線段來繪制不完整的多邊形,但這是BasicStroke.JOIN_BEVEL的問題不適用於線段。 這是我使用線段的方法:

//polygon is not Java's Polygon, my own implementation, and the methods do as
//they imply
for(int i = 0; i < polygon.getNumberOfPoints(); i++){
    Point2D.Double first = polygon.getPoint(i);
    Point2D.Double second = new Point2D.Double();
    if(polygon.getPoint(i+1) != null){
        second = polygon.getPoint(i+1);
        trans1 = /* some graphic translation of first */
        trans2 = /* some graphic translation of second */
        g.setColor(polygon.getColor());
        g.setStroke(new BasicStroke(polygon.getWeight(), BasicStroke.JOIN_BEVEL, BasicStroke.CAP_BUTT));
        g.draw(new Line2D.Double(trans1[0], trans1[1], trans2[0], trans2[1]));
    }
}

這工作正常,但並不能完全按照我的意願工作。 g.setStroke(/*stuff here*/); 對關節沒有影響。

好吧,我完全錯過了一種方法。

g.drawPolyline(int[] xCoords, int[] yCoords, int numPoints)

這解決了我的問題。

創建一個Path2D.Double,但不要調用closePath()。

Path2D.Double path = new Path2D.Double();
for (int i = 0; i < polygon.getNumberOfPoints(); i++) {
  Point2D.Double point = polygon.getPoint(i);
  trans1 = /* some graphic translation */;
  if (i == 0)
    path.moveTo(trans1[0], trans1[1]);
  else
    path.lineTo(trans1[0], trans2[0]);
}
g.setColor(polygon.getColor());
g.setStroke(new BasicStroke(polygon.getWeight(), BasicStroke.JOIN_BEVEL, BasicStroke.CAP_BUTT));
g.draw(path);

暫無
暫無

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

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