Draw a semi ring - JavaFX

后端 未结 4 1778
梦毁少年i
梦毁少年i 2020-12-11 03:47

I would like to know how to draw a semi circle in JavaFX. I tried to use Shape and QuadCurve but I couldn\'t make a perfect semicircle.

Here is a picture of what I\'

4条回答
  •  一整个雨季
    2020-12-11 04:30

    Path.arcTo() the parameter SweepAngle refers to the rotation degree, if sweepAngle is positive the arc is clockwise, if sweepAngle is negative the arc is counterclockwise.

    This code is used in my production environment, it draws a semi-circle ring using a bitmap image, the path goes clockwise on the outer radius, and counter-clockwise on the inner radius:

    drawpercent = 0.85; //this draws a semi ring to 85% you can change it using your code.
    DegreesStart = -90;
    DegreesRotation = 180;
    
    radiusPathRectF = new android.graphics.RectF((float)CentreX - (float)Radius, (float)CentreY - (float)Radius,  (float)CentreX + (float)Radius, (float)CentreY + (float)Radius);
    innerradiusPathRectF = new android.graphics.RectF((float)CentreX - (float)InnerRadius, (float)CentreY - (float)InnerRadius, (float)CentreX + (float)InnerRadius, (float)CentreY + (float)InnerRadius);
    
    Path p = new Path(); //TODO put this outside your draw() function,  you should never have a "new" keyword inside a fast loop.
    
                    degrees = (360 + (DegreesStart)) % 360;
                    radians = (360 - degrees + 90) * Math.PI / 180.0;
                    //radians = Math.toRadians(DegreesStart);
                    int XstartOuter = (int)Math.round((Math.cos(radians) * Radius + CentreX));
                    int YstartOuter = (int)Math.round((Math.sin(-radians)* Radius + CentreY));
                    int XstartInner = (int)Math.round((Math.cos(radians) * InnerRadius + CentreX));
                    int YstartInner = (int)Math.round((Math.sin(-radians) * InnerRadius + CentreY));
    
                    degrees = (360 + (DegreesStart + drawpercent * DegreesRotation)) % 360;
                    //radians = degrees * Math.PI / 180.0;
                    radians = (360 - degrees + 90) * Math.PI / 180.0;
                    //radians = Math.toRadians(DegreesStart + drawpercent * DegreesRotation);
                    int XendOuter = (int)Math.round((Math.cos(radians) * Radius + CentreX));
                    int YendOuter = (int)Math.round((Math.sin(-radians) * Radius + CentreY));
                    int XendInner = (int)Math.round((Math.cos(radians) * InnerRadius + CentreX));
                    int YendInner = (int)Math.round((Math.sin(-radians) * InnerRadius + CentreY));
    
                    //draw a path outlining the semi-circle ring.
                    p.moveTo(XstartInner, YstartInner);
                    p.lineTo(XstartOuter, YstartOuter);
                    p.arcTo(radiusPathRectF, (float)DegreesStart - (float)90, (float)drawpercent * (float)DegreesRotation);
                    p.lineTo(XendInner, YendInner);
                    p.arcTo(innerradiusPathRectF, (float)degrees - (float)90, -1 * (float)drawpercent * (float)DegreesRotation);
                    p.close();
    
                    g.clipPath(p);
    
                    g.drawBitmap(bitmapCircularBarImage, bitmapRect0, bitmapRectXY, paint);
    

提交回复
热议问题