Java 图形 2d 避免折线扭曲的角

Java Graphics 2d avoid Polyline distorted corners

我正在开发用于绘制地铁地图的图形界面。一条线用站点表示为圆圈,一条折线到 link them.You 可以用 mouseDrag 移动站点,当然它会实时更新显示的地图。我的问题是当站点到达某个角度时,会出现折线变形,并且由 2 条线创建的角在站点圆显示之外,我想知道是否有办法避免这种情况。

screenshots of the app with the polyline issue

这是我绘制折线的代码

//x and y point array creation
    xPoints = new int[this.stationViews.size()];
    yPoints = new int[this.stationViews.size()];
    for (int i=0;i<this.stationViews.size();i++) {
        //fill arrays with the center point of circles representing stations
        xPoints[i] = this.stationViews.get(i).getStation().getPosX()-this.stationViews.size()/2;
        yPoints[i] = this.stationViews.get(i).getStation().getPosY()-this.stationViews.size();
    }

    //setting color
    g2D.setColor(this.line.getColor());

    //set stroke width relative to the zoom level
    int strokeWidth=5;
    if(!this.stationViews.isEmpty()) {
    if (this.stationViews.get(0).getStationSize()>14) {
        strokeWidth = this.stationViews.get(0).getStationSize()-13;
    }else {
        strokeWidth = 3;
    }
    } 
    g2D.setStroke(new BasicStroke(strokeWidth));



    //draw the polyline
    if (this.stationViews.size() >1) {
    g2D.drawPolyline(xPoints, yPoints, this.stationViews.size());
    }
    //draw the station (g2D.drawCircle)
    for (StationView stationView : stationViews) {
        stationView.affiche(g2D,this.line.getColor());
    }

感谢您的帮助

也就是所谓的斜接。您似乎默认使用 JOIN_MITER、sharp 连接末尾的延伸线,对于小角度,它可以指向远离连接的地方。

g2d.setStroke(new BasicStroke(strokeWidth,
    BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 5));

miter a surface forming the beveled end or edge of a piece where a joint is made by cutting two pieces at an angle and fitting them together.

它也是尖顶的主教帽,因此得名。