如何在给定的形状中旋转LinearGradient? [英] How to rotate the LinearGradient in a given Shape?
问题描述
我尝试找到旋转嵌套在例如中的LinearGradient
对象的方法Rectangle
对象,说:
Rectangle rect = new Rectangle(0, 0, 200, 200);
LinearGradient lg = new LinearGradient(0, 0, 100, 0, false, CycleMethod.REPEAT, new Stop[] {
new Stop(0, Color.BLACK);
new Stop(0.5, Color.WHITE);
new Stop(1, Color.BLACK);
});
rect.setFill(lg);
现在,我尝试旋转此lg
对象,例如,向左旋转45度,但不旋转整个rect
.有什么方法可以实现?
提供给LinearGradient
构造函数的第一个参数分别是渐变轴的起点和终点的坐标.这意味着您只需通过适当旋转的轴即可获得旋转"渐变.
以最简单的形式,对于您描述的示例,可以使用以下模式:
double angleInRadians = Math.toRadians(45);
double length = 100;
double endX = Math.cos(angleInRadians) * length;
double endY = Math.sin(angleInRadians) * length;
LinearGradient lg = new LinearGradient(0, 0, endX, endY, ...);
这将导致渐变旋转45度.
此处的固定值将与其他参数一起影响渐变的最终外观.以您的示例为例,该渐变具有与以前相同的波长"(即100
),并且在左上角以相同的颜色开始(即Color.BLACK
将位于坐标(0,0)
).>
I try to find the way to rotate the LinearGradient
object nested into e.g. Rectangle
object, say:
Rectangle rect = new Rectangle(0, 0, 200, 200);
LinearGradient lg = new LinearGradient(0, 0, 100, 0, false, CycleMethod.REPEAT, new Stop[] {
new Stop(0, Color.BLACK);
new Stop(0.5, Color.WHITE);
new Stop(1, Color.BLACK);
});
rect.setFill(lg);
Now, I try to rotate this lg
object, for example for 45 degrees to the left, but without rotating the whole rect
. Is there any way to achieve that?
The first parameters that are given to the LinearGradient
constructor are the coordinates of the start- and end point of the gradient axis, respectively. This means that you can achieve a "rotated" gradient simply by passing in an appropriately rotated axis.
In the simplest form, for the example that you described, you can use the following pattern:
double angleInRadians = Math.toRadians(45);
double length = 100;
double endX = Math.cos(angleInRadians) * length;
double endY = Math.sin(angleInRadians) * length;
LinearGradient lg = new LinearGradient(0, 0, endX, endY, ...);
This will result in a gradient rotated by 45 degrees.
The fixed values here will affect the final appearance of the gradient, together with the other parameters. Referring to your example, this gradient with the same "wave length" as before (namely 100
), and start with the same color at the upper left corner (i.e. Color.BLACK
will be at coordinates (0,0)
).
这篇关于如何在给定的形状中旋转LinearGradient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!