Add shape to JavaFX Path - Java JavaFX

Java examples for JavaFX:Shape

Description

Add shape to JavaFX Path

Demo Code

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {
    Application.launch(args);/*w ww  . j  a  va  2 s .com*/
  }

  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 306, 550, Color.WHITE);

    Path path = new Path();

    MoveTo moveTo = new MoveTo();
    moveTo.setX(150);
    moveTo.setY(150);

    QuadCurveTo quadCurveTo = new QuadCurveTo();
    quadCurveTo.setX(150);
    quadCurveTo.setY(140);
    quadCurveTo.setControlX(100);
    quadCurveTo.setControlY(150);

    LineTo lineTo1 = new LineTo();
    lineTo1.setX(50);
    lineTo1.setY(150);

    LineTo lineTo2 = new LineTo();
    lineTo2.setX(100);
    lineTo2.setY(25);

    LineTo lineTo3 = new LineTo();
    lineTo3.setX(150);
    lineTo3.setY(250);
    path.getElements().add(moveTo);
    path.getElements().add(quadCurveTo);
    path.getElements().add(lineTo1);
    path.getElements().add(lineTo2);
    path.getElements().add(lineTo3);
    path.setTranslateY(30);
    path.setStrokeWidth(3);
    path.setStroke(Color.BLACK);

    root.getChildren().add(path);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

Related Tutorials