JavaFX Arc

0

JavaFX Arc Tutorial:

JavaFX Arc class creates an arc with center, angle and radius.

To Creates an Arc we need center, start angle, angular angle(difference between start and ending angles) and an arc type.

Constructors :

  • Arc()
    Creates an empty instance of Arc.
  • Arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)
    Creates a new instance of Arc as given parameters.

JavaFX Arc Types :

  1. ArcType.OPEN: Indicates that you want to draw just the arc itself.
  2. ArcType.CHORD: Indicates that you want to draw the arc and then connect the ends with a straight line to create a closed shape.
  3. ArcType.ROUND: Indicates that you want to use straight lines to connect the ends to the center of the circle.

Creating Arc :


Arc arc = new Arc();
        //setting perameters using setter methods.
            arc.setCenterX(120.0f);
            arc.setCenterY(120.0f);
            arc.setRadiusX(50.0f);
            arc.setRadiusY(50.0f);
            arc.setStartAngle(75.0f);
            arc.setLength(290.0f);
            arc.setType(ArcType.ROUND);

Example of arc:


package javafxtuts;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;

/**
 *
 * @author JavaFXtuts.com
 */
public class Javafxtuts extends Application {
    
    @Override
    public void start(Stage primaryStage) {
         Pane root = new Pane();
         
        //creating circle using empty constructor 
        Arc arc = new Arc();
        
            arc.setCenterX(120.0f);
            arc.setCenterY(120.0f);
            arc.setRadiusX(50.0f);
            arc.setRadiusY(50.0f);
            arc.setStartAngle(75.0f);
            arc.setLength(290.0f);
            arc.setType(ArcType.ROUND);
            
        // To set outlier color of arc to Black
        arc.setStroke(Color.DARKGREEN);
        
        // To set interior color of arc to DARKCYAN
        arc.setFill(Color.DARKCYAN);
        //To set the width of the stroke using setStrokeWidth()
        arc.setStrokeWidth(3);
        root.getChildren().addAll(arc);
        Scene scene = new Scene(root, 250, 220);
        
        primaryStage.setTitle("javafxtuts.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output:

Explain :

  • Frist create a new instance of arc and then set the parameter using setter methods as
    Arc arc = new Arc();
  •             arc.setCenterX(120.0f);
                arc.setCenterY(120.0f);

    It set the center at (120,120).

  •       arc.setRadiusX(50.0f);
                arc.setRadiusY(50.0f);

    It set the radius to 50.

  • arc.setStartAngle(75.0f);
    It set start angle of arc is 75 degrees.
  • arc.setLength(290.0f);
    It set ending angle to 290+75 =365 =5.
    So it Start from 75 degree and end at 5 degrees.
  • arc.setType(ArcType.ROUND);
    It set the type of the arc to the round type.
  • And finally change the color of stroke.
Share.

Leave A Reply