我要做的是使用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*/);
对关节没有影响。
答案 0 :(得分:3)
嗯,我完全错过了一种方法。
g.drawPolyline(int[] xCoords, int[] yCoords, int numPoints)
这解决了我的问题。
答案 1 :(得分:0)
创建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);