Search code examples
javajavafxclipradial-gradients

Clipping a rectangle filled with radial gradient to make corners round


Environment:

I want to make some blue light in my JavaFX application UI:

When I try to make that using a rectangle with rounded corners and radial gradient fill, this (see picture) corner isn't transparent.

BUT If I will change rectangle's fill to someone solid color (ex: Color.RED) then this corner will be transparent and will be rendered correctly... Why it doesn't working in the case with radial gradient fill?

Now I using this code:

RadialGradient gradient = new RadialGradient(
        0D, 0D, 34D, 250D, 231D, false, CycleMethod.NO_CYCLE,
        new Stop(0D, Color.rgb(0, 102, 254, 0.2D)),
        new Stop(100D, Color.rgb(0, 102, 254, 0D))
);

Rectangle light = new Rectangle(280D, 280D);
// I've tried to make 'light' rectangle corners round:
// light.setArcWidth(14D);
// light.setArcHeight(14D);
light.setFill(gradient);

// ... and also tried to make some new clip with round corners
Rectangle clip = new Rectangle(280D, 280D);
clip.setArcWidth(14D);
clip.setArcHeight(14D);

// ... and apply that to the 'light' rectangle, but it hasn't an effect :(
light.setClip(clip);

// after that light will be a children node of the parent StackPane
// this StackPane is a parent element of the all application 
// (provided to the Scene instance)

Solution

  • Lol, I am sorry :D

    I don't know before that I should to use arc parameters as corner radius * 2. I've just use 28 instead of 14 in the arc width and arc height and it's a solution of my problem.