Talk:Draw Gradient in Java ME
This article provides a useful class for creating linear gradient paint effects in Java ME. The standard Graphics class used in Canvas and GameCanvas-based applications does not allow for the easy creation of gradients. The code example provided addresses this limitation by allowing programmers to create rectangles filled with either vertical and horizontal gradients. The programmer is simply required to provide the two-colours required for the gradient, the coordinates of the top-left corner and the width and height of the rectangle. The class then takes care of painting the gradient, interpolating between the two colours in order to smoothly transition from one to the other.
The code in this class provides for reasonably smooth gradients in Java ME and is very useful given the limitations of the standard Graphics class. This approach is better than using an image containing a gradient as a background, as it is difficult to scale an image correctly for all possible screen aspect ratios. Images also increase JAR file size.
This article is good explicated with methods and with an example in an easy way. This article shows how to draw gradient on canvas. gradientBox() method addresses some calculation needed for setting gradient on to canvas.
And these functions are applied in paint method defined in GradientRectCanvas wrap.