”;
An arc in simple geometry is defined as a portion of a circumference of an ellipse or a circle. Or, simply put, it is a curve that is joins two end points. An arc also encloses an angle less than or equal to 360 degrees at the centre of the circle. It is described by the following properties −
-
length − The distance along the arc.
-
angle − The angle the curve makes at the centre of the circle.
-
radiusX − The width of the full Ellipse of which the current arc is a part of.
-
radiusY − The height of the full Ellipse of which the current arc is a part of.
If both radiusX and radiusY are same, then the arc is a part of a circle circumference.
Arc in JavaFX
In JavaFX, an arc is represented by a class named Arc. This class belongs to the package javafx.scene.shape.
By instantiating this class, you can create an arc node in JavaFX.
This class has a few properties of the double datatype namely −
-
centerX − The x coordinate of the center of the arc.
-
centerY − The y coordinate of the center of the arc.
-
radiusX − The width of the full ellipse of which the current arc is a part of.
-
radiusY − The height of the full ellipse of which the current arc is a part of.
-
startAngle − The starting angle of the arc in degrees.
-
length − The angular extent of the arc in degrees.
To draw an arc, you need to pass values to these properties, either by passing them to the constructor of this class, in the same order, at the time of instantiation, or, by using their respective setter methods.
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.
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 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 inside the start() method of Application class as shown below.
public class ClassName extends Application { public void start(Stage primaryStage) throws Exception { //Creating an object of the class Arc Arc arc = new Arc(); } }
Step 2: 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.
//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);
Step 3: Setting the Type of the Arc
You can also set the type of the arc (round, chord open) by using the setType() method as shown in the following code block.
//Setting the type of the arc arc.setType(ArcType.ROUND);
Step 4: Adding Arc Object to Group
In the start() method, instantiate a Group class by passing the arc object, which was created in the previous step, as a parameter value to its constructor −
Group root = new Group(arc);
Step 5: Launching Application
Once the 2D object is created, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
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 { 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 --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample.java java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample
Output
On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.
Example
Let us try to draw another arc of type “Open” in the following example. Save this code in a file with the name ArcOpen.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 ArcOpen extends Application { 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.OPEN); //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 Open 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 --module-path %PATH_TO_FX% --add-modules javafx.controls ArcOpen.java java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcOpen
Output
On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.
Example
Now we will try to draw another arc of type “Chord” in the following example. Save this code in a file with the name ArcChord.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 ArcChord extends Application { 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.CHORD); //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 a Chord 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 --module-path %PATH_TO_FX% --add-modules javafx.controls ArcChord.java java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcChord
Output
On executing, the above program generates a JavaFX window displaying an arc as shown in the following screenshot.
”;