fill Sharp Vertical Gradient micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

fill Sharp Vertical Gradient 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 fillSharpVerticalGradient(Graphics g, int objX,
            int objY, int objWidth, int objHeight, int color1, int color2,
            int color3, int color4, int divPrc) {
        int objHeightHalf = (objHeight * divPrc) / 100;
        fillVerticalGradient(g, objX, objY, objWidth, objHeightHalf + 1,
                color1, color2);/*ww  w.j  ava 2 s. com*/
        objY += objHeightHalf;
        fillVerticalGradient(g, objX, objY, objWidth, objHeight
                - objHeightHalf, color3, color4);
    }
    public static void fillVerticalGradient(Graphics g, int objX, int objY,
            int objWidth, int objHeight, int color1, int color2) {
        if (color1 == color2) {
            g.setColor(color1);
            g.fillRect(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);
            if (isLightGraphics) {
                /** Drawing rectangle for middle color **/
                g.setColor((sRed__ - pRed__) / 2 + pRed__,
                        (sGreen - pGreen) / 2 + pGreen, (sBlue_ - pBlue_)
                                / 2 + pBlue_);
                g.fillRect(objX, objY, objWidth, objHeight);
            } else {
                /** Drawing gradient **/
                for (int y = 0; y < objHeight; y++) {
                    g.setColor((sRed__ - pRed__) * y / objHeight + pRed__,
                            (sGreen - pGreen) * y / objHeight + pGreen,
                            (sBlue_ - pBlue_) * y / objHeight + pBlue_);
                    g.drawLine(objX, objY + y, objX + objWidth - 1, objY
                            + y);
                }
            }
        }
    }
}

Related Tutorials