Thursday, May 26, 2016

12. Path

With a Path object, we can create a complicated shape.


Here we use 2 QuadCurveTo objects, 1 CubicCurveTo object, and 2 ArcTo objects. In addition, we set a starting point, for the entire Path, using MoveTo. Unlike the nonpath objects with similar names, we only give ending point, as the start point is given by last path object.


package ex12;

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

public class Ex12 extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Path path = new Path();
        
        MoveTo st = new MoveTo(100,200);
        QuadCurveTo q1 = new QuadCurveTo();
        q1.setControlX(100); q1.setControlY(100);
        q1.setX(200); q1.setY(100);

        CubicCurveTo c = new CubicCurveTo();
        c.setControlX1(200); c.setControlY1(0);
        c.setControlX2(300); c.setControlY2(0);
        c.setX(300); c.setY(100);
        
        QuadCurveTo q2 = new QuadCurveTo();
        q2.setControlX(400); q2.setControlY(100);
        q2.setX(400); q2.setY(200);
        
        ArcTo a1 = new ArcTo();
        a1.setRadiusX(150); a1.setRadiusY(150);
        a1.setX(250); a1.setY(350);
        
        ArcTo a2 = new ArcTo();
        a2.setRadiusX(150); a2.setRadiusY(150);
        a2.setX(100); a2.setY(200);
        
        path.setFill(Color.CORAL);
        path.getElements().addAll(st, q1, c, q2, a1, a2);
        path.setStrokeWidth(10);
        path.setStroke(Color.LIGHTSEAGREEN);
        
        Group root = new Group();
        root.getChildren().addAll(path);
        
        Scene scene = new Scene(root, 500, 400, Color.HONEYDEW);
        
        primaryStage.setTitle("Example 12. Path");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

This is the output:


No comments:

Post a Comment