How to rotate the LinearGradient in a given Shape?

前端 未结 1 464
半阙折子戏
半阙折子戏 2021-01-12 12:39

I try to find the way to rotate the LinearGradient object nested into e.g. Rectangle object, say:

Rectangle rect = new Rectangle(0,         


        
相关标签:
1条回答
  • 2021-01-12 13:31

    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)).

    0 讨论(0)
提交回复
热议问题