How to Create the path element quadratic curve in JavaFX?


This is class represents the path element quadratic curve. It helps you to draw a quadratic curve form the current coordinates to the specified (new) coordinates.

To create a line path element −

  • Instantiate the QuadCurveTo class.

  • Set values to the properties of this class using setter methods or, bypassing them to the constructor.

  • Instantiate the Path class.

  • Get the observable list object of the above-created Path using the getElements() method.

  • Add the above created QuadCurveTo object to the observable list using the add() method.

  • Finally, add the path to the Group object.

Example

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.scene.shape.VLineTo;
public class QuadraticCurveToExample extends Application {
   public void start(Stage stage) {
      //Creating PathElement objects
      MoveTo moveTo = new MoveTo(15, 15);
      LineTo line1 = new LineTo(100, 150);
      //Instantiating the class QuadCurve
      QuadCurveTo quadCurveTo = new QuadCurveTo();
      //Setting properties of the class QuadCurve
      quadCurveTo.setX(500.0f);
      quadCurveTo.setY(220.0f);
      quadCurveTo.setControlX(250.0f);
      quadCurveTo.setControlY(0.0f);
      //Creating the HLineTo object
      VLineTo vLine = new VLineTo();
      vLine.setY(80);
      //Creating a Path
      Path path = new Path();
      path.getElements().addAll(moveTo, line1, quadCurveTo, vLine);
      //Setting other properties
      path.setStrokeWidth(8.0);
      path.setStroke(Color.DARKSLATEGREY);
      //Preparing the Stage object
      Group root = new Group(path);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("JavaFX Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output


Updated on: 13-Apr-2020

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements