Paints a smooth gradient in a rectangle given the bounds x, y, width, height. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Paints a smooth gradient in a rectangle given the bounds x, y, width, height.

Demo Code


//package com.java2s;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.SwingConstants;

public class Main {
    /**/*from w  w w . j a  va  2  s .  c om*/
     * Paints a smooth gradient in a rectangle given the bounds x, y, width, height.  
     * This defaults to drawing a horizontal gradient.
     * @param g the Graphics instance
     * @param x the x position
     * @param y the y position
     * @param width the width of the region
     * @param height the height of the region
     * @param startColor the start color of the gradient
     * @param endColor the end color of the gradient
     */
    public static void paintGradient(Graphics g, int x, int y, int width,
            int height, Color startColor, Color endColor) {
        paintGradient(g, x, y, width, height, startColor, endColor,
                SwingConstants.HORIZONTAL);
    }

    /**
     * Paints a smooth gradient in a rectangle given the bounds x, y, width, height and orientation. 
     * @param g the Graphics instance
     * @param x the x position
     * @param y the y position
     * @param width the width of the region
     * @param height the height of the region
     * @param startColor the start color of the gradient
     * @param endColor the end color of the gradient
     * @param orientation the orientation of the gradient - either horizontal or vertical
     */
    public static void paintGradient(Graphics g, int x, int y, int width,
            int height, Color startColor, Color endColor, int orientation) {
        double radix = height;
        if (orientation == SwingConstants.VERTICAL)
            radix = width;
        double redDelta = (endColor.getRed() - startColor.getRed()) / radix;
        double greenDelta = (endColor.getGreen() - startColor.getGreen())
                / radix;
        double blueDelta = (endColor.getBlue() - startColor.getBlue())
                / radix;
        double alphaDelta = (endColor.getAlpha() - startColor.getAlpha())
                / radix;
        Color c = startColor;
        double currentRed = redDelta;
        double currentBlue = blueDelta;
        double currentGreen = greenDelta;
        double currentAlpha = alphaDelta;
        int end = height;
        if (orientation == SwingConstants.VERTICAL)
            end = width;
        for (int i = 0; i < end; i++) {
            g.setColor(c);
            if (orientation == SwingConstants.HORIZONTAL)
                g.drawLine(x, y + i, width, y + i);
            else
                g.drawLine(x + i, y, x + i, height);
            int red = (int) (startColor.getRed() + currentRed);
            int green = (int) (startColor.getGreen() + currentGreen);
            int blue = (int) (startColor.getBlue() + currentBlue);
            int alpha = (int) (startColor.getAlpha() + currentAlpha);
            currentRed = currentRed + redDelta;
            currentBlue = currentBlue + blueDelta;
            currentGreen = currentGreen + greenDelta;
            currentAlpha = currentAlpha + alphaDelta;
            c = new Color(red, green, blue, alpha);
        }
    }
}

Related Tutorials