Java Color Inverse fillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h)

Here you can find the source of fillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h)

Description

fill Inverse Hor Gradient

License

Open Source License

Declaration

public static void fillInverseHorGradient(Graphics g, Color[] colors,
            int x, int y, int w, int h) 

Method Source Code

//package com.java2s;
/*/*ww w. j a v  a 2 s  .c  om*/
 * Copyright 2005 MH-Software-Entwicklung. All rights reserved.
 * Use is subject to license terms.
 */

import java.awt.*;

public class Main {
    public static void fillInverseHorGradient(Graphics g, Color[] colors,
            int x, int y, int w, int h) {
        int steps = colors.length;
        double dy = (double) h / (double) steps;
        if (dy <= 3.001) {
            int y1 = y;
            for (int i = 0; i < steps; i++) {
                int y2 = y + (int) Math.round((double) i * dy);
                g.setColor(colors[colors.length - i - 1]);
                if (i == (steps - 1)) {
                    g.fillRect(x, y1, w, y + h - y1);
                } else {
                    g.fillRect(x, y1, w, y2 - y1);
                }
                y1 = y2;
            }
        } else {
            smoothFillInverseHorGradient(g, colors, x, y, w, h);
        }

    }

    public static void smoothFillInverseHorGradient(Graphics g,
            Color[] colors, int x, int y, int w, int h) {
        Graphics2D g2D = (Graphics2D) g;
        int steps = colors.length;
        double dy = (double) h / (double) steps;
        int y1 = y;
        for (int i = 0; i < steps; i++) {
            int y2 = y + (int) Math.round((double) i * dy);
            g.setColor(colors[colors.length - i - 1]);
            if (i == (steps - 1)) {
                g2D.setPaint(null);
                g2D.setColor(colors[colors.length - i - 1]);
                g.fillRect(x, y1, w, y + h - y1);
            } else {
                g2D.setPaint(new GradientPaint(0, y1, colors[colors.length
                        - i - 1], 0, y2, colors[colors.length - i - 2]));
                g.fillRect(x, y1, w, y2 - y1);
            }
            y1 = y2;
        }
    }
}

Related

  1. inverse(Color c)
  2. inverse(Color color)