×
Namespaces

Variants
Actions
(Difference between revisions)

Draw Gradient in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
jappit (Talk | contribs)
(j2me ui gradient)
 
jappit (Talk | contribs)
Line 2: Line 2:
  
 
Here is a J2ME class for gradients drawing, supporting both horizontal and vertical gradients.
 
Here is a J2ME class for gradients drawing, supporting both horizontal and vertical gradients.
 +
 +
[[Image:J2me_gradients.jpg]]
  
 
This could be useful to substitute background gradient images with graphics drawn by code.
 
This could be useful to substitute background gradient images with graphics drawn by code.

Revision as of 21:10, 11 April 2008


Here is a J2ME class for gradients drawing, supporting both horizontal and vertical gradients.

J2me gradients.jpg

This could be useful to substitute background gradient images with graphics drawn by code.

package com.jappit.wiki.gradientrect.graphics;
 
import javax.microedition.lcdui.Graphics;
 
public class Gradient
{
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
 
public static void gradientBox(Graphics g, int color1, int color2, int left, int top, int width, int height, int orientation)
{
int max = orientation == VERTICAL ? height : width;
 
for(int i = 0; i < max; i++)
{
int color = midColor(color1, color2, 100 * (max - 1 - i) / (max - 1));
 
g.setColor(color);
 
if(orientation == VERTICAL)
g.drawLine(left, top + i, left + width - 1, top + i);
else
g.drawLine(left + i, top, left + i, top + height - 1);
}
}
 
static int midColor(int color1, int color2, int prop)
{
int red =
(((color1 >> 16) & 0xff) * prop +
((color2 >> 16) & 0xff) * (100 - prop)) / 100;
 
int green =
(((color1 >> 8) & 0xff) * prop +
((color2 >> 8) & 0xff) * (100 - prop)) / 100;
 
int blue =
(((color1 >> 0) & 0xff) * prop +
((color2 >> 0) & 0xff) * (100 - prop)) / 100;
 
int color = red << 16 | green << 8 | blue;
 
return color;
}
}
206 page views in the last 30 days.
×