We know that there are a class named RadialGradientPaint
in Java and we can use it to have a gradient painting for circle.
But I want to have an oval (ellipse) GradientPaint
. How to implement oval GradientPaint
?
We know that there are a class named RadialGradientPaint
in Java and we can use it to have a gradient painting for circle.
But I want to have an oval (ellipse) GradientPaint
. How to implement oval GradientPaint
?
RadialGradientPaint
provides two ways to paint itself as an ellipse instead of a circle:Upon construction, you can specify a transform for the gradient. For example, if you provide the following transform:
AffineTransform.getScaleInstance(0.5, 1)
, your gradient will be an upright oval (the x dimension will be half that of the y dimension).Or, you can use the constructor that requires a
Rectangle2D
be provided. An appropriate transform will be created to make the gradient ellipse bounds match that of the provided rectangle. I found the class documentation helpful: RadialGradientPaint API. In particular, see the documentation for this constructor.Use an
AffineTransform
when drawing theRadialGradientPaint
. This would require a scale instance of the transform. It might end up looking something like this:Original image: The original static (boring) 'screen shot' of the app.