如何使用JavaFX创建三次贝塞尔曲线?
三次贝塞尔曲线是两个变量的三次多项式函数。
在JavaFX中,三次贝塞尔曲线由**javafx.scene.shape.CubicCurve**类表示。此类包含八个属性,它们是:
**startX** - 此属性表示曲线的起点的x坐标。您可以使用**setStartX()**方法为此属性设置值。
**startY** - 此属性表示曲线的起点的y坐标。您可以使用**setStartY()**方法为此属性设置值。
**controlX1**:此属性表示曲线的第一个控制点的x坐标。您可以使用**setControlX1()**方法为此属性设置值。
**controlY1** - 此属性表示曲线的第一个控制点的y坐标。您可以使用**setControlY1()**方法为此属性设置值。
**controlX2** - 此属性表示曲线的第二个控制点的x坐标。您可以使用**setControlX2()**方法为此属性设置值。
**controlY2** - 此属性表示曲线的第二个控制点的y坐标。您可以使用**setControlY2()**方法为此属性设置值。
**endX** - 此属性表示曲线终点的x坐标。您可以使用**setEndX()**方法为此属性设置值。
**endY** - 此属性表示曲线终点的y坐标。您可以使用**setEndY()**方法为此属性设置值。
要创建一个圆,您需要:
实例化此类。
使用setter方法设置所需的属性,或者将它们作为参数传递给构造函数。
将创建的节点(形状)添加到Group对象。
示例
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.CubicCurve;
public class DrawingCubicCurve extends Application {
@Override
public void start(Stage stage) {
//Drawing a cubic curve
CubicCurve cubicCurve = new CubicCurve();
//Setting properties to cubic curve
cubicCurve.setStartX(75.0f);
cubicCurve.setStartY(75.0f);
cubicCurve.setControlX2(250.0f);
cubicCurve.setControlY2(250.0f);
cubicCurve.setControlX1(400.0f);
cubicCurve.setControlY1(40.0f);
cubicCurve.setEndX(500.0f);
cubicCurve.setEndY(260.0f);
//Setting other properties
cubicCurve.setFill(Color.CHOCOLATE);
cubicCurve.setStrokeWidth(8.0);
cubicCurve.setStroke(Color.BROWN);
//Setting the scene object
Group root = new Group(cubicCurve);
Scene scene = new Scene(root, 600, 300);
stage.setTitle("Drawing a cubic curve");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP