java - javafx中带有渐变颜色的矩形
问题描述
我在我的 javafx 应用程序中使用锚窗格。我想画一个矩形并用这样的渐变颜色填充它:矩形的左侧是蓝色,右侧是红色,我希望它看起来从左到右,蓝色减少,红色增加。
我知道如何放置一个矩形(如何在 javafx 中使用 Rectangle 类),但我不知道如何以这种方式填充它。有任何想法吗?
解决方案
请参阅有关线性渐变的文档。
public class Gradient extends Application {
public static final double S = 100;
@Override
public void start(Stage stage) {
Stop[] stops = new Stop[] {
new Stop(0, Color.BLUE),
new Stop(1, Color.RED)
};
LinearGradient gradient = new LinearGradient(
0, 0,
1, 0,
true,
CycleMethod.NO_CYCLE,
stops
);
Rectangle rectangle = new Rectangle(S, S, gradient);
stage.setScene(new Scene(new Group(rectangle)));
stage.show();
}
public static void main(String[] args) {
launch();
}
}
推荐阅读
- c - 如何从 Windows 上的套接字端口获取 PID?
- ios - 关闭文本字段的键盘时 UIView 动画不起作用
- javascript - 如何将 Swagger 添加到现有的 Node 应用程序?
- css - TranslateX 打破了粘性导航栏
- c++ - 如何使用 auto 深度声明几个变量?
- c# - 填充数据适配器时字典中不存在给定的键
- c++ - 如何每次都在不同大小的循环中重新声明相同的数组?
- xml - 如何使用此架构使此 xml 文件有效
- excel - Mac上的VBA Excel Mac字典类获取Keys()属性
- google-maps - 如何重绘与用户绘制的完全相同的多边形(超过 4 个顶点)?