I think the result can be improved. The number "100" should be a parameter in midColor(). If you have a gradient from black to white, the current code gives you 100 steps, but 256 are possible.
Thanks for the hint! I've modified the code to fix this issue. Jappit 10:45, 9 June 2008 (EEST)
I made a mutable image and fill it with gradient color, using your class. but I want it transparent.
Any idea?
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.
--Larry101 12:26, 19 September 2009 (UTC)
Level: Intermediate
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.
--Vkmunjpara 21:10, 22 September 2009 (UTC)