JavaFX Path

0

JavaFX Path Tutorial:

  • The Path Class creates a simple geometric Path.
  • The class with suffix of “To” are Path elements.
  • The Starting Point of path is define by MoveTo().
  • The structure of path may be any type.

Creating Path:

  • Path path = new Path();
    First create a new instance of Path.
  • Now create MoveTo instance from where particular path is going.
    MoveTo moveTo = new MoveTo();
    moveTo.setX(10.0f);
    moveTo.setY(10.0f);
  • After that structure of path using path elements as Line, Arc and CubicCurve etc.
  • LineTo lineTo=new LineTo(180.0f, 180.0f);
    It Create a path follows a line start from moveTo point and end at lineTo point.

Constructors :

  • Path()
    Creates an empty instance of Path.
  • Path(PathElement… elements)
    Creates a new instance of Path.

Example of javafx path:


package javafxtuts;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

/**
 *
 * @author JavaFXtuts.com
 */
public class Javafxtuts extends Application {
    
    @Override
    public void start(Stage primaryStage) {
         Pane root = new Pane();
         
        //creating path using empty constructor 
        Path path = new Path();
        // Creating a MoveTo for starting point of the path
         MoveTo moveTo = new MoveTo();
         //setting (x,y) coordinates of stsrting point(MoveTo) using setter methods
            moveTo.setX(10.0f);
            moveTo.setY(10.0f);   
        
            //To Create a path which follows a line using LineTO 
            // And add the last ponts of path
        LineTo lineTo=new LineTo(180.0f, 180.0f);
        //add moveTo and lineTo to the path
        path.getElements().addAll(moveTo,lineTo);
        
        
        root.getChildren().addAll(path);
        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 :

Now for creating path like an arc we use the “ArcTo” path element.
In ArcTo path element we needs the last point and radius of arc.

Example of path like arc:



package javafxtuts;


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

/**
 *
 * @author JavaFXtuts.com
 */
public class Javafxtuts extends Application {
    
    @Override
    public void start(Stage primaryStage) {
         Pane root = new Pane();
         
        //creating path using empty constructor 
        Path path = new Path();
        // Creating a MoveTo for starting point of the path
         MoveTo moveTo = new MoveTo();
         //setting (x,y) coordinates of starting point(MoveTo) using setter methods
            moveTo.setX(20.0f);
            moveTo.setY(10.0f);   
        
            //To Create a path which follows an Arc using ArcTO 
            // And add the last points of path and radius of arc
        ArcTo arcTo = new ArcTo();
            arcTo.setX(100.0f);
            arcTo.setY(100.0f);
            arcTo.setRadiusX(80.0f);
            arcTo.setRadiusY(80.0f);

        //add moveTo and lineTo to the path
        path.getElements().addAll(moveTo,arcTo);
        
        
        root.getChildren().addAll(path);
        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 :

Share.

Leave A Reply