JavaFX Tutorial - JavaFX Curve








Cubic Curve

To create a cubic curve, use the appropriate constructor.

A cubic curve's main parameters to set are startX, startY, controlX1 (control point1 X), controlY1 (control point1 Y), controlX2 (control point2 X), and controlY2 (control point2 Y), endX, endY.

The startX, startY, endX, and endY parameters are the starting and ending points of a curved line. controlX1, controlY1, controlX2, and controlY2 are control points.

The control point (controlX1, controlY1) influences the line segment between the start point (startX, startY) and the midpoint of the line.

The control point (controlX2, controlY2) influences the line segment between the midpoint of the line and its end point (endX, endY).

A control point pulls the curve toward the direction of itself.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CubicCurve;
import javafx.stage.Stage;
/*from  ww w  . j ava  2  s.c o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    stage.setTitle("ComboBoxSample");
    Scene scene = new Scene(new Group(), 450, 250);

    CubicCurve cubic = new CubicCurve();
    cubic.setStartX(0.0f);
    cubic.setStartY(50.0f);
    cubic.setControlX1(25.0f);
    cubic.setControlY1(0.0f);
    cubic.setControlX2(75.0f);
    cubic.setControlY2(100.0f);
    cubic.setEndX(100.0f);
    cubic.setEndY(50.0f);
 

    Group root = (Group) scene.getRoot();
    root.getChildren().add(cubic);
    stage.setScene(scene);
    stage.show();
  }
}

The code above generates the following result.

null




QuadCurve

The javafx.scene.shape.QuadCurve class is similar to the cubic curve. Instead of two control points we only have one control point for QuadCurve.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.QuadCurve;
import javafx.stage.Stage;
/*from  w  ww . j  a  v a2  s.  c o  m*/
public class Main extends Application {
  @Override
  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);
    stage.setTitle("");


    QuadCurve quad = new QuadCurve();
    quad.setStartX(0.0f);
    quad.setStartY(50.0f);
    quad.setEndX(50.0f);
    quad.setEndY(50.0f);
    quad.setControlX(25.0f);
    quad.setControlY(0.0f);
    
    root.getChildren().add(quad);

    scene.setRoot(root);
    stage.show();
  }

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

The code above generates the following result.

null