fill Gradient Rect - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

fill Gradient Rect

Demo Code


import java.awt.Color;
import java.awt.Graphics;

public class Main{
    public static void fillGradientRect(Graphics g, Color c1, Color c2,
            int x, int y, int width, int height, GradientMode mode) {
        switch (mode) {
        case VERTICAL: {
            double[] step = getColorSteppers(c1, c2, (double) height);
            for (int i = 0; i < height; ++i) {
                if (!((int) (c1.getAlpha() + step[3] * i) == 0)) {
                    g.setColor(new Color((int) (c1.getRed() + step[0] * i),
                            (int) (c1.getGreen() + step[1] * i), (int) (c1
                                    .getBlue() + step[2] * i), (int) (c1
                                    .getAlpha() + step[3] * i)));
                    g.drawLine(x, y + i, x + width - 1, y + i);
                }//from   ww  w .  jav  a 2  s . c o m
            }
            break;
        }
        case HORIZONTAL: {
            double[] step = getColorSteppers(c1, c2, (double) width);
            ;
            for (int i = 0; i < width; ++i) {
                if (!((int) (c1.getAlpha() + step[3] * i) == 0)) {
                    g.setColor(new Color((int) (c1.getRed() + step[0] * i),
                            (int) (c1.getGreen() + step[1] * i), (int) (c1
                                    .getBlue() + step[2] * i), (int) (c1
                                    .getAlpha() + step[3] * i)));
                    g.drawLine(x + i, y, x + i, y + height - 1);
                }
            }
            break;
        }
        case DOWNLEFT: {
            if (height == width
                    || (height == width - 1 || height == width + 1)) {
                int[] step = getColorSteppers(c1, c2, height * 2);
                Color halfColor = new Color(c2.getRGB());
                for (int i = 0; i < height - 1; ++i) {
                    halfColor = new Color(c1.getRed() + step[0] * i,
                            c1.getGreen() + step[1] * i, c1.getBlue()
                                    + step[2] * i, c1.getAlpha() + step[3]
                                    * i);
                    g.setColor(halfColor);
                    g.drawLine(x + height - 1 - i, y, x + height - 1, y + i);
                }
                step = getColorSteppers(halfColor, c2, height);
                for (int i = 0; i < height; ++i) {
                    g.setColor(new Color(halfColor.getRed() + step[0] * i,
                            halfColor.getGreen() + step[1] * i, halfColor
                                    .getBlue() + step[2] * i, halfColor
                                    .getAlpha() + step[3] * i));
                    g.drawLine(x + height - 1 - i, y + height - 1, x, y + i);
                }
            }
            break;
        }
        case DOWNRIGHT: {
            if (height == width
                    || (height == width - 1 || height == width + 1)) {
                int[] step = getColorSteppers(c1, c2, height * 2);
                Color halfColor = new Color(c2.getRGB());
                for (int i = 0; i < height - 1; ++i) {
                    halfColor = new Color(c1.getRed() + step[0] * i,
                            c1.getGreen() + step[1] * i, c1.getBlue()
                                    + step[2] * i, c1.getAlpha() + step[3]
                                    * i);
                    g.setColor(halfColor);
                    g.drawLine(x + i, y, x, y + i);
                }
                step = getColorSteppers(halfColor, c2, height);
                for (int i = 0; i < height; ++i) {
                    g.setColor(new Color(halfColor.getRed() + step[0] * i,
                            halfColor.getGreen() + step[1] * i, halfColor
                                    .getBlue() + step[2] * i, halfColor
                                    .getAlpha() + step[3] * i));
                    g.drawLine(x + height - 1, y + i, x + i, y + height - 1);
                }
            }
            break;
        }
        case CENTER: {
            if (height == width
                    || (height == width - 1 || height == width + 1)) {
                int[] step = getColorSteppers(c1, c2, height / 2);
                for (int i = 0; i <= height / 2; ++i) {
                    g.setColor(new Color(c1.getRed() + step[0] * i, c1
                            .getGreen() + step[1] * i, c1.getBlue()
                            + step[2] * i, c1.getAlpha() + step[3] * i));
                    g.drawRect(x + height / 2 - i, y + height / 2 - i,
                            i * 2, i * 2);
                }
            }
            break;
        }
        }
    }
    public static int[] getColorSteppers(Color c1, Color c2, int dividend) {
        int r = 0, g = 0, b = 0, a = 0;
        r = (c2.getRed() - c1.getRed()) / dividend;
        g = (c2.getGreen() - c1.getGreen()) / dividend;
        b = (c2.getBlue() - c1.getBlue()) / dividend;
        a = (c2.getAlpha() - c1.getAlpha()) / dividend;
        return new int[] { r, g, b, a };
    }
    public static double[] getColorSteppers(Color c1, Color c2,
            double dividend) {
        double r = 0, g = 0, b = 0, a = 0;
        r = (c2.getRed() - c1.getRed()) / dividend;
        g = (c2.getGreen() - c1.getGreen()) / dividend;
        b = (c2.getBlue() - c1.getBlue()) / dividend;
        a = (c2.getAlpha() - c1.getAlpha()) / dividend;
        return new double[] { r, g, b, a };
    }
}

Related Tutorials