JavaFX路徑

JavaFX有其他內置的形狀,如:

  • Arc
  • Circle
  • CubicCurve
  • Ellipse
  • Line
  • Path
  • Polygon
  • Polyline
  • QuadCurve
  • Rectangle
  • SVGPath
  • Text

以下代碼顯示瞭如何創建路徑(Path)。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
// @ w  w w .Y I I b A i. CO M
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Checkbox Sample");
        stage.setWidth(230);
        stage.setHeight(120);

        Path path = new Path();
        path.getElements().add(new MoveTo(0.0f, 50.0f));
        path.getElements().add(new LineTo(100.0f, 100.0f));

        VBox vbox = new VBox();
        vbox.getChildren().addAll(path);
        vbox.setSpacing(5);

        HBox root = new HBox();
        root.getChildren().add(vbox);
        root.setSpacing(40);
        root.setPadding(new Insets(20, 10, 10, 20));

        ((Group) scene.getRoot()).getChildren().add(root);

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

上面的代碼生成以下結果。
JavaFX路徑

Path元素實際上從javafx.scene.shape.PathElement類擴展,它僅在Path對象的上下文中使用。

所以不能實例化一個LineTo類放在場景圖中。使用To作爲後綴的類是Path元素,而不是Shape節點。

例如,MoveTo和LineTo對象實例可添加到Path對象的Path元素中,而不可以添加到場景的形狀。

示例-1

以下代碼顯示瞭如何將QuadCurveTo添加到路徑。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.stage.Stage;
// at  w w W .Y I ib  AI.c  OM
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("");

    Path path = new Path();

    MoveTo moveTo = new MoveTo();
    moveTo.setX(0.0f);
    moveTo.setY(50.0f);

    QuadCurveTo quadTo = new QuadCurveTo();
    quadTo.setControlX(25.0f);
    quadTo.setControlY(0.0f);
    quadTo.setX(50.0f);
    quadTo.setY(50.0f);

    path.getElements().add(moveTo);
    path.getElements().add(quadTo);

    root.getChildren().add(path);

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

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

上面的代碼生成以下結果。
JavaFX路徑

實例-2

使用PathMoveToCubicCurveTo創建曲線 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

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);

    Path path = new Path();

    MoveTo moveTo = new MoveTo();
    moveTo.setX(0.0f);
    moveTo.setY(0.0f);

    CubicCurveTo cubicTo = new CubicCurveTo();
    cubicTo.setControlX1(0.0f);
    cubicTo.setControlY1(0.0f);
    cubicTo.setControlX2(100.0f);
    cubicTo.setControlY2(100.0f);
    cubicTo.setX(100.0f);
    cubicTo.setY(50.0f);

    path.getElements().add(moveTo);
    path.getElements().add(cubicTo);


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

上面的代碼生成以下結果。

JavaFX路徑

實例-3

減去兩個形狀以創建路徑,如下代碼 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.DropShadowBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);// from W  w w.y Ii   b A I .c O M
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Shapes");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 300, Color.WHITE);

        Ellipse bigCircle = EllipseBuilder.create()
                .centerX(100)
                .centerY(100)
                .radiusX(50)
                .radiusY(75/2)
                .strokeWidth(3)
                .stroke(Color.BLACK)
                .fill(Color.WHITE)
                .build();

        Ellipse smallCircle = EllipseBuilder.create()
                .centerX(100)
                .centerY(100)
                .radiusX(35/2)
                .radiusY(25/2)

                .build();

        Shape shape = Path.subtract(bigCircle, smallCircle);
        shape.setStrokeWidth(1);
        shape.setStroke(Color.BLACK);

        shape.setFill(Color.rgb(255, 200, 0));

        root.getChildren().add(shape);

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

上面的代碼生成以下結果。

JavaFX路徑

實例-4

使用VLineTo創建垂直線,如下代碼所示 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.VLineTo;
import javafx.stage.Stage;
// by  Ww W.Y I IB A i .C o  m 
public class Main extends Application {

    @Override
    public void start(final Stage stage) {
        stage.setTitle("HTML");
        stage.setWidth(500);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());

        VBox root = new VBox();     

        Path path = new Path();
        path.getElements().add(new MoveTo(50.0f, 0.0f));
        path.getElements().add(new VLineTo(50.0f));

        root.getChildren().addAll(path);
        scene.setRoot(root);

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

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

上面的代碼生成以下結果。

JavaFX路徑