Namespaces
Variants
Actions
Revision as of 08:42, 8 December 2011 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Desenho gradiente, em Java ME

Jump to: navigation, search
Dados do artigo

Artigo
Tradução:
Última alteração feita por hamishwillee em 08 Dec 2011


Aqui está uma classe J2ME para desenhos gradientes, suportando tanto grandiente horizontal quanto vertical.

J2me gradients.jpg

Esto pode ser muito útil para substituir imagens gradientes de background(fundo) com gráficos escritos por código.

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, max * (max - 1 - i) / (max - 1), max);
 
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 max)
{
int red =
(((color1 >> 16) & 0xff) * prop +
((color2 >> 16) & 0xff) * (max - prop)) / max;
 
int green =
(((color1 >> 8) & 0xff) * prop +
((color2 >> 8) & 0xff) * (max - prop)) / max;
 
int blue =
(((color1 >> 0) & 0xff) * prop +
((color2 >> 0) & 0xff) * (max - prop)) / max;
 
int color = red << 16 | green << 8 | blue;
 
return color;
}
}

E aqui está um simples Canvas usando o método gradientBox (o efeito final é mostrado no screenshot no início deste artigo):

package com.jappit.wiki.gradientrect.display;
 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
 
import com.jappit.wiki.gradientrect.graphics.Gradient;
 
public class GradientRectCanvas extends Canvas {
 
protected void paint(Graphics g)
{
int halfWidth = getWidth() / 2;
int halfHeight = getHeight() / 2;
 
Gradient.gradientBox(g, 0xffffff, 0xff0000, 0, 0, halfWidth, halfHeight, Gradient.HORIZONTAL);
 
Gradient.gradientBox(g, 0xff0000, 0xffffff, halfWidth, 0, halfWidth, halfHeight, Gradient.VERTICAL);
 
Gradient.gradientBox(g, 0xffff00, 0x00ffff, 0, halfHeight, halfWidth, halfHeight, Gradient.VERTICAL);
 
Gradient.gradientBox(g, 0x00ff00, 0x0000ff, halfWidth, halfHeight, halfWidth, halfHeight, Gradient.VERTICAL);
}
 
}
132 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved