JavaFX | CubicCurve 示例
CubicCurve 是 JavaFX 的一部分。CubiCurve 类在 (x, y) 坐标空间中定义了一个三次贝塞尔参数曲线段。三次曲线经过起点和终点,也经过两个控制点。控制点被指定为贝塞尔控制点。
该类的构造函数是:
- CubicCurve() : 创建一个新的三次曲线实例
- CubicCurve(double startX, double startY, double controlX1, double controlY1, double controlX2, double controlY2, double endX, double endY) :创建具有指定起点和终点以及两个控制点的三次曲线实例。
常用方法
Method | Explanation |
---|---|
getControlX1() | returns the x coordinate of 1st control point |
getControlY1() | returns the y coordinate of 1st control point |
getControlX2() | returns the x coordinate of 2nd control point |
getControlY2() | returns the y coordinate of 2nd control point |
getEndX() | returns the x coordinate for the end point |
getEndY() | returns the y coordinate for the end point |
getStartX() | returns the x coordinate for the start point |
getStartY() | returns the y coordinate for the start point |
setControlX1(double value) | sets the x coordinate for the 1st control point |
setControlY1(double value) | sets the y coordinate for the 1st control point |
setControlX2(double value) | sets the x coordinate for the 2nd control point |
setControlY2(double value) | sets the y coordinate for the 2nd control point |
setEndX(double value) | sets the x coordinate for the end point |
setEndY(double value) | sets the y coordinate for the end point |
setStartX(double value) | sets the x coordinate for the start point |
setStartY(double value) | sets the y coordinate for the start point |
创建三次曲线的Java程序
该程序创建一个名为cubic_curve 的CubicCurve(两个控制点,起点和终点作为参数传递)。 CubicCurve 将在场景中创建,而场景又将托管在舞台中。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加cubic_curve。该组附加到场景。最后调用 show() 方法显示最终结果。
// Java program to create a cubic curve
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.CubicCurve;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class cubic_curve_0 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating cubic_curve");
// create a cubic_curve
CubicCurve cubic_curve = new CubicCurve(10.0f, 10.0f, 200.0f, 140.0f, 120.0f, 240.0f, 160.0f, 70.0f);
// create a Group
Group group = new Group(cubic_curve);
// translate the cubic_curve to a position
cubic_curve.setTranslateX(100);
cubic_curve.setTranslateY(100);
// create a scene
Scene scene = new Scene(group, 500, 300);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
输出:
用于创建三次曲线并为三次曲线设置填充的Java程序
这个程序创建一个名为cubic_curve的CubicCurve(两个控制点,起点和终点使用setControlX1(),setControlY1(),setControlX2(),setControlY2(),setStartX(),setStartY(),setEndX() ,以及 setEndY() 函数)。 CubicCurve 将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加cubic_curve。该组附加到场景。最后调用 show() 方法显示最终结果。函数setFill() 用于设置三次曲线的填充。
// Java program to create a cubic curve and set a fill for cubic curve
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.CubicCurve;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.paint.Color;
public class cubic_curve_1 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating cubic_curve");
// create a cubic_curve
CubicCurve cubic_curve = new CubicCurve();
// set start
cubic_curve.setStartX(10.0f);
cubic_curve.setStartY(10.0f);
// set control coordinates
cubic_curve.setControlX1(200.0f);
cubic_curve.setControlY1(140.0f);
cubic_curve.setControlX2(120.0f);
cubic_curve.setControlY2(240.0f);
// set end coordinates
cubic_curve.setEndX(160.0f);
cubic_curve.setEndY(70.0f);
// create a Group
Group group = new Group(cubic_curve);
// translate the cubic_curve to a position
cubic_curve.setTranslateX(100);
cubic_curve.setTranslateY(100);
// set fill for the cubic curve
cubic_curve.setFill(Color.BLUE);
// create a scene
Scene scene = new Scene(group, 500, 300);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器
参考:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/CubicCurve.html