CycleMethod类是JavaFX的一部分。CycleMethod定义在渐变边界外绘制时要使用的方法。它包含3个枚举常量,如下所示:
null
- 无周期 :用于定义使用端子颜色填充剩余区域的循环方法。
- 反映 :用于定义反映渐变颜色的循环方法,从开始到结束,然后从结束到开始。
- 重复 :用于定义重复渐变颜色以填充剩余区域的循环方法。
常用方法:
方法 | 解释 |
---|---|
valueOf(字符串名称) | 返回指定名称的CycleMethod。 |
价值观() | 返回包含Cyclemethod类型值的数组。 |
以下程序说明了CycleMethod类的使用:
- Java程序创建LinearGradient对象,向其添加停止点,将CycleMethod设置为repeat,将Propirative设置为false,并将其应用于矩形:
- 在这个程序中,我们将创建一个停止对象数组,其偏移值范围为0到1。然后创建具有指定停止点的LinearGradient对象。
- 将CycleMethod设置为repeat(重复),并将其比例设置为false(假)。然后创建一个具有指定x、y位置和半径的圆,并向其添加线性渐变。
- 然后创建一个VBox并设置其对齐方式。
- 将圆圈添加到 vbox 并添加 vbox 将场景添加到舞台并调用 show() 函数来显示结果。
// Java program to create a LinearGradient object,
// add stops to it, set the CycleMethod to repeat,
// set proportional to false and apply it to the
// rectangle
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.layout.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
import
javafx.geometry.*;
import
javafx.scene.layout.*;
import
javafx.scene.shape.*;
import
javafx.scene.paint.*;
public
class
CycleMethod_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"CycleMethod"
);
// create stops
Stop[] stop = {
new
Stop(
0
, Color.RED),
new
Stop(
1
, Color.BLUE)};
// create a Linear gradient object
LinearGradient linear_gradient =
new
LinearGradient(
0
,
0
,
35
,
0
,
false
, CycleMethod.REPEAT, stop);
// create a rectangle
Rectangle rectangle =
new
Rectangle(
100
,
100
,
100
,
70
);
// set fill
rectangle.setFill(linear_gradient);
// create VBox
VBox vbox =
new
VBox(rectangle);
// ste Alignment
vbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(vbox,
400
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- Java程序创建LinearGradient对象,为其添加停止点,将CycleMethod设置为reflect,将其设置为false,并将其应用于圆:
- 在这个程序中,我们将创建一个停止对象数组,其偏移值范围为0到1。
- 然后创建具有指定停止点的LinearGradient对象。将CycleMethod设置为reflect,并将其设置为false。
- 创建一个具有指定x、y位置和半径的圆,并向其添加线性渐变。创建VBox并设置其对齐方式。
- 将圆添加到vbox,将vbox添加到场景,并将场景添加到舞台。
- 打电话给 show() 函数来显示结果。
// Java program to create a LinearGradient object,
// add stops to it, set the CycleMethod to reflect,
// set proportional to false and apply it to the
// circle
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.layout.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
import
javafx.geometry.*;
import
javafx.scene.layout.*;
import
javafx.scene.shape.*;
import
javafx.scene.paint.*;
public
class
CycleMethod_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"CycleMethod"
);
// create stops
Stop[] stop = {
new
Stop(
0
, Color.RED),
new
Stop(
1
, Color.BLUE)};
// create a Linear gradient object
LinearGradient linear_gradient =
new
LinearGradient(
0
,
0
,
35
,
0
,
false
, CycleMethod.REFLECT, stop);
// create a rectangle
Rectangle rectangle =
new
Rectangle(
100
,
100
,
100
,
70
);
// set fill
rectangle.setFill(linear_gradient);
// create VBox
VBox vbox =
new
VBox(rectangle);
// ste Alignment
vbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(vbox,
400
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- Java程序创建LinearGradient对象,向其添加停止点,将CycleMethod设置为no cycle,将Propirative设置为false,并将其应用于矩形:
- 在这个程序中,我们将创建一个停止对象数组,其偏移值范围为0到1。
- 创建具有指定停止点的LinearGradient对象。
- 将CycleMethod设置为no cycle(无循环),并将Propirative(成比例)设置为false(假)。然后创建一个具有指定x、y位置和半径的圆,并向其添加线性渐变。创建VBox并设置其对齐方式。
- 现在将圆圈添加到vbox,将vbox添加到场景,将场景添加到舞台,并调用 show() 函数来显示结果。
// Java program to create LinearGradient object,
// add stops to it, set the CycleMethod to no
// cycle, set proportional to false and apply
// it to the rectangle
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.layout.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
import
javafx.geometry.*;
import
javafx.scene.layout.*;
import
javafx.scene.shape.*;
import
javafx.scene.paint.*;
public
class
CycleMethod_3
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"CycleMethod"
);
// create stops
Stop[] stop = {
new
Stop(
0
, Color.RED),
new
Stop(
1
, Color.BLUE)};
// create a Linear gradient object
LinearGradient linear_gradient =
new
LinearGradient(
0
,
0
,
35
,
0
,
false
, CycleMethod.NO_CYCLE, stop);
// create a rectangle
Rectangle rectangle =
new
Rectangle(
100
,
100
,
100
,
70
);
// set fill
rectangle.setFill(linear_gradient);
// create VBox
VBox vbox =
new
VBox(rectangle);
// ste Alignment
vbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(vbox,
400
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。
参考: https://docs.oracle.com/javafx/2/api/javafx/scene/paint/CycleMethod.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END