如何在 JavaFX 中创建路径元素三次曲线?


这个类表示路径元素三次曲线。它可以帮助你从当前坐标绘制一个三次曲线方程到指定的(新)坐标。

要创建一个线条路径元素,-

  • 实例化CubicCurve 类。

  • 使用 setter 方法或通过构造函数绕过它们来设置此类的属性值。

  • 实例化 Path 类。

  • 使用 getElements() 方法获取以上创建的 Path 的可观察列表对象。

  • 使用 add() 方法将上面创建的 CubicCurve 对象添加到可观察列表中。

  • 最后,将路径添加到组对象。

示例

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.CubicCurveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.VLineTo;
public class CubicCurveExample 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 CubicCurve
      CubicCurveTo cubicCurveTo = new CubicCurveTo();
      //Setting properties of the class CubicCurve
      cubicCurveTo.setControlX1(400.0f);
      cubicCurveTo.setControlY1(40.0f);
      cubicCurveTo.setControlX2(175.0f);
      cubicCurveTo.setControlY2(250.0f);
      cubicCurveTo.setX(500.0f);
      cubicCurveTo.setY(150.0f);
      //Creating the HLineTo object
      VLineTo vLine = new VLineTo();
      vLine.setY(80);
      //Creating a Path
      Path path = new Path();
      path.getElements().addAll(moveTo, line1, cubicCurveTo, 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);
   }
}

输出

更新于: 2020 年 4 月 13 日

202 次浏览

开启您的 职业生涯

完成课程,获得证书

入门
广告
© . All rights reserved.