draw Horizontal Gradient Border micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

draw Horizontal Gradient Border micro edition

Demo Code


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Main{
    public static boolean isLightGraphics = false;
    /** Primary colors **/
    private static int pRed__ = 0;
    private static int pGreen = 0;
    private static int pBlue_ = 0;
    /** Secondary colors **/
    private static int sRed__ = 0;
    private static int sGreen = 0;
    private static int sBlue_ = 0;
    public static void drawHorizontalGradientBorder(Graphics g, int objX,
            int objY, int objWidth, int objHeight, int color1, int color2) {
        if (color1 == color2) {
            g.setColor(color1);//from  w ww.  j av a2s  . c om
            g.drawRect(objX, objY, objWidth, objHeight);
        } else {
            /** Primary colors **/
            pRed__ = (color1 & 0xFF0000) >> 16;
            pGreen = (color1 & 0x00FF00) >> 8;
            pBlue_ = (color1 & 0x0000FF);
            /** Secondary colors **/
            sRed__ = (color2 & 0xFF0000) >> 16;
            sGreen = (color2 & 0x00FF00) >> 8;
            sBlue_ = (color2 & 0x0000FF);
            objHeight--;
            objWidth--;
            if (isLightGraphics) {
                /** Drawing rectangle for middle color **/
                g.setColor((sRed__ - pRed__) / 2 + pRed__,
                        (sGreen - pGreen) / 2 + pGreen, (sBlue_ - pBlue_)
                                / 2 + pBlue_);
                g.drawRect(objX, objY, objWidth, objHeight);
            } else {
                /** Drawing vertical lines **/
                g.setColor(color1);
                g.drawLine(objX, objY, objX, objY + objHeight);
                g.setColor(color2);
                g.drawLine(objX + objWidth, objY, objX + objWidth, objY
                        + objHeight);
                /** Drawing horizontal lines **/
                for (int x = 0; x < objWidth; x++) {
                    g.setColor((sRed__ - pRed__) * x / objWidth + pRed__,
                            (sGreen - pGreen) * x / objWidth + pGreen,
                            (sBlue_ - pBlue_) * x / objWidth + pBlue_);
                    g.drawLine(objX + x, objY, objX + x, objY);
                    g.drawLine(objX + x, objY + objHeight, objX + x, objY
                            + objHeight);
                }
            }
        }
    }
}

Related Tutorials