【问题标题】:Java Graphics 2d avoid Polyline distorted cornersJava Graphics 2d 避免折线扭曲角
【发布时间】:2020-02-15 12:11:50
【问题描述】:

我正在开发用于绘制地铁地图的图形界面。一条线用车站表示为圆圈和一条折线来连接它们。您可以用鼠标拖动移动车站,当然它会实时更新显示的地图。我的问题是,当车站到达某个角度时,会出现折线变形,并且由 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());
    }

感谢您的帮助

【问题讨论】:

    标签: java graphics2d polyline


    【解决方案1】:

    这就是所谓的斜接。您似乎默认使用 JOIN_MITER,sharp 在末端连接延长线,这可能会在小角度的连接处指向远处。

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

    斜接表面形成斜面的末端或边缘,通过以一定角度切割两片并将它们装配在一起来形成接头。

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

    【讨论】:

      猜你喜欢
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2010-10-15
      • 2018-09-24
      • 2018-07-03
      • 2014-12-05
      • 1970-01-01
      相关资源
      最近更新 更多