0

How to create semi circle with adjustable start and end angle in JavaFX. I tried to use Arc or ArcTo but it never gives me what I need. Is there any easy solution to create it?

How it should look like:

https://i.stack.imgur.com/ZQu35.png

Aerus
  • 4,332
  • 5
  • 43
  • 62
Mike Sany
  • 5
  • 1
  • 6
  • Take a look at [this question](http://stackoverflow.com/questions/11719005/draw-a-semi-ring-javafx/11720870#11720870), it looks quite similar and also uses `arcTo`. Once you have one of them, the others can be easily obtained by rotation. – Aerus Apr 29 '15 at 20:41
  • I started at this solution but it creates only semi half circle. But there is no option to set start and end angle like -45,45. As you said I need only one and then use rotation on rest of them. – Mike Sany Apr 29 '15 at 20:58
  • I'm not sure what you mean with the start and end angle – Aerus Apr 29 '15 at 23:10

1 Answers1

0

A quick solution would be to create an outer circle, an inner circle, 2 lines and use Shape.subtract to create a new shape:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        try {

            Pane root = new Pane();

            double dist = 10;
            double outerRadius = 100;
            double innerRadius = 50;

            Circle outerCircle = new Circle();
            outerCircle.setRadius(outerRadius);

            Circle innerCircle = new Circle();
            innerCircle.setRadius(innerRadius);

            Line topLeftBottomRightLine = new Line(-outerRadius, -outerRadius, outerRadius, outerRadius);
            topLeftBottomRightLine.setStrokeWidth(dist);

            Line bottomLeftTopRightLine = new Line(-outerRadius, outerRadius, outerRadius, -outerRadius);
            bottomLeftTopRightLine.setStrokeWidth(dist);

            Shape shape = Shape.subtract(outerCircle, innerCircle);
            shape = Shape.subtract(shape, topLeftBottomRightLine);
            shape = Shape.subtract(shape, bottomLeftTopRightLine);

            shape.setStroke(Color.BLUE);
            shape.setFill(Color.BLUE.deriveColor(1, 1, 1, 0.3));

            shape.relocate(300, 100);

            root.getChildren().addAll(shape);

            Scene scene = new Scene(root, 800, 400);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

Similarly you can create only parts of the shape by using an arc and subtracting an inner circle from it.

Roland
  • 18,114
  • 12
  • 62
  • 93