JavaFX - 2D Shapes Types of Arc



In JavaFX, you can draw three kinds of arc’s namely −

  • Open − An arc which is not closed at all is known as an open arc.

  • Chord − A chord is a type of an arc which is closed by straight line.

  • Round − The Round arc is an arc which is closed by joining the starting and ending point to the center of the ellipse.

Open Closed Round

You can set the type of the arc using the method setType() by passing any of the following properties − ArcType.OPEN, ArcType.CHORD, ArcType.Round.

Steps to Draw Arc

To Draw an arc in JavaFX, follow the steps given below.

Step 1: Creating a Class

Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class as follows.

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {      
   }    
} 

Step 2: Creating an Arc

You can create an arc in JavaFX by instantiating the class named Arc which belongs to a package javafx.scene.shape. You can instantiate this class as shown below.

//Creating an object of the class Arc         
Arc arc = new Arc();

Step 3: Setting Properties to the Arc

Specify the x, y coordinates of the center of the Ellipse (of which this arc is a part of). These coordinates include – radiusX, radiusY, start angle and length of the arc using their respective setter methods as shown in the following code block.

You can also set the type of the arc (round, chord open) by using the setType() method.

//Setting the properties of the arc 
arc.setCenterX(300.0f); 
arc.setCenterY(150.0f); 
arc.setRadiusX(90.0f); 
arc.setRadiusY(90.0f); 
arc.setStartAngle(40.0f); 
arc.setLength(239.0f); 
arc.setType(ArcType.ROUND);

Step 4: Setting the Type of the Arc

You can set the type of the arc using the method setType() as shown in the following code block.

//Setting the type of the arc 
arc.setType(ArcType.ROUND);

Step 5: Creating a Group Object

In the start() method create a group object by instantiating the class named Group, which belongs to the package javafx.scene.

Pass the Arc (node) object created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as shown below −

Group root = new Group(arc);

Step 6: Creating a Scene Object

Create a Scene by instantiating the class named Scene which belongs to the package javafx.scene. To this class pass the Group object (root) created in the previous step.

In addition to the root object, you can also pass two double parameters representing height and width of the screen along with the object of the Group class as follows.

Scene scene = new Scene(group ,600, 300);

Step 7: Setting the Title of the Stage

You can set the title to the stage using the setTitle() method of the Stage class. The primaryStage is a Stage object which is passed to the start method of the scene class as a parameter.

Using the primaryStage object, set the title of the scene as Sample Application as follows.

primaryStage.setTitle("Sample Application");

Step 8: Adding Scene to the Stage

You can add a Scene object to the stage using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method as follows.

primaryStage.setScene(scene); 

Step 9: Displaying the Contents of the Stage

Display the contents of the scene using the method named show() of the Stage class as follows.

primaryStage.show();

Step 10: Launching the Application

Launch the JavaFX application by calling the static method launch()of the Application class from the main method as follows.

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

Example

Following is a program which generates an arc. Save this code in a file with the name ArcExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.stage.Stage; 
import javafx.scene.shape.Arc;  
import javafx.scene.shape.ArcType; 
        
public class ArcExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing an arc 
      Arc arc = new Arc(); 
         
      //Setting the properties of the arc 
      arc.setCenterX(300.0f); 
      arc.setCenterY(150.0f); 
      arc.setRadiusX(90.0f); 
      arc.setRadiusY(90.0f); 
      arc.setStartAngle(40.0f); 
      arc.setLength(239.0f);  
      
      //Setting the type of the arc 
      arc.setType(ArcType.ROUND);         
         
      //Creating a Group object  
      Group root = new Group(arc); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing an Arc"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage
      stage.show(); 
   }   
   public static void main(String args[]){ 
      launch(args); 
   } 
}

Compile and execute the saved Java file from the command prompt using the following commands.

javac ArcExample.java 
java ArcExample

On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.

Drawing Arc
javafx_2d_shapes.htm
Advertisements