Using the Rectangle fill method.
Example of Linear gradient, fillRect method:
Example of Radial gradient,fillRect method:
Example of linear gradient strokeRect method: