ArcTo Path Object

The Path Element Arc is used to draw an arc to a point in the specified coordinates from the current position.

It is represented by a class named ArcTo. This class belongs to the package javafx.scene.shape.

This class has 4 properties of the double datatype namely −

  • X − The x coordinate of the center of the arc.
  • Y − 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.

To draw the Path element arc, you need to pass values to these properties This can be done 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.

Steps to draw PathElement Arc

To draw an arc to a specified point from the current position in JavaFX, follow the steps given below.

Step 1: Creating a Path Object

Create a Path object by instantiating the Path class inside the start() method of Application class as shown below.

publicclassClassNameextendsApplication{@Overridepublicvoidstart(Stage primaryStage)throwsException{//Creating a Path object Path path =newPath();}}

Step 2: Create a Path

Create the MoveTo path element and set the XY coordinates to the starting point of the line to the coordinates (100, 150). This can be done using the methods setX() and setY() of the class MoveTo as shown below.

//Moving to the starting point MoveTo moveTo =newMoveTo(); 
moveTo.setX(100.0f); 
moveTo.setY(150.0f);

Step 3: Creating an Object of the Class ArcTo

Create the path element quadratic curve by instantiating the class named ArcTo, which belongs to the package javafx.scene.shape as shown below −

//Creating an object of the class ArcTo  ArcTo arcTo =newArcTo()

Step 4: Setting Properties to the Arc Element

Specify the x, y coordinates of the center of the ellipse (of which this arc is a part of). Then you can specify the radiusX, radiusY, start angle, and length of the arc using their respective setter methods as shown below.

//setting properties of the path element arc  
arcTo.setX(300.0); 
arcTo.setY(50.0); 
       
arcTo.setRadiusX(50.0); 
arcTo.setRadiusY(50.0);

Step 5: Adding Elements to the Observable List of Path Class

Add the path elements MoveTo and arcTo, created in the previous steps to the observable list of the Path class as follows −

//Adding the path elements to Observable list of the Path class   
path.getElements().add(moveTo); 
path.getElements().add(cubicCurveTo);

Step 6: Launching Application

Once the Arc path 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 that draws an arc from the current point to a specified position using the class Path of JavaFX. Save this code in a file with the name ArcExample.java.

importjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.stage.Stage;importjavafx.scene.shape.ArcTo;importjavafx.scene.shape.MoveTo;importjavafx.scene.shape.Path;publicclassArcExampleextendsApplication{@Overridepublicvoidstart(Stage stage){//Creating an object of the class Path  Path path =newPath();//Moving to the starting point MoveTo moveTo =newMoveTo(); 
      moveTo.setX(250.0); 
      moveTo.setY(250.0);//Instantiating the arcTo class ArcTo arcTo =newArcTo();//setting properties of the path element arc  
      arcTo.setX(300.0); 
      arcTo.setY(50.0); 
       
      arcTo.setRadiusX(50.0); 
      arcTo.setRadiusY(50.0);//Adding the path elements to Observable list of the Path class 
      path.getElements().add(moveTo); 
      path.getElements().add(arcTo);//Creating a Group object  Group root =newGroup(path);//Creating a scene object Scene scene =newScene(root,600,300);//Setting title to the Stage
      stage.setTitle("Drawing an arc through a path");//Adding scene to the stage 
      stage.setScene(scene);//Displaying the contents of the stage 
      stage.show();}publicstaticvoidmain(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, which is drawn from the current position to the specified point, as shown below.

Drawing Arc Path

Example

Let us try to draw an ArcTo Path in JavaFX to create a pendulum object with a circular pendulum moving to and fro on an Arc Path. Save this code in a file with the name ArcToAnimation.java.

importjavafx.application.Application;importjavafx.animation.PathTransition;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.stage.Stage;importjavafx.scene.shape.*;importjavafx.scene.shape.MoveTo;importjavafx.scene.shape.Path;importjavafx.util.Duration;publicclassArcToAnimationextendsApplication{@Overridepublicvoidstart(Stage stage){// Drawing a circleCircle circle =newCircle(300.0f,50.0f,40.0f);//Creating an object of the class Path  Path path =newPath();//Moving to the starting point MoveTo moveTo =newMoveTo(); 
      moveTo.setX(250.0); 
      moveTo.setY(250.0);//Instantiating the arcTo class ArcTo arcTo =newArcTo();//setting properties of the path element arc  
      arcTo.setX(300.0); 
      arcTo.setY(50.0); 
       
      arcTo.setRadiusX(50.0); 
      arcTo.setRadiusY(50.0);//Adding the path elements to Observable list of the Path class 
      path.getElements().add(moveTo); 
      path.getElements().add(arcTo);//Creating a path transition PathTransition pathTransition =newPathTransition();//Setting the duration of the path transition 
      pathTransition.setDuration(Duration.millis(1000));//Setting the node 
      pathTransition.setNode(circle);//Setting the path 
      pathTransition.setPath(path);//Setting the orientation of the path 
      pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);//Setting the cycle count for the transition 
      pathTransition.setCycleCount(50);//Setting auto reverse value to true 
      pathTransition.setAutoReverse(true);//Playing the animation 
      pathTransition.play();//Creating a Group object  Group root =newGroup();
      root.getChildren().addAll(circle, path);//Creating a scene object Scene scene =newScene(root,600,300);//Setting title to the Stage
      stage.setTitle("Drawing an arc through a path");//Adding scene to the stage 
      stage.setScene(scene);//Displaying the contents of the stage 
      stage.show();}publicstaticvoidmain(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 a pendulum path as shown below.

Drawing Pendulum

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *