Fills given the upper and lower halves of the given rectangle in gradient style from bright to dark colors. - Java 2D Graphics

Java examples for 2D Graphics:Color Dark

Description

Fills given the upper and lower halves of the given rectangle in gradient style from bright to dark colors.

Demo Code


//package com.java2s;

import java.awt.*;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<Integer, GradientPaint> gpCache = null;
    private static final boolean noGpCache = Boolean
            .getBoolean("netbeans.winsys.nogpcache");

    /**//from  ww  w. j a  v  a2  s .c  o m
     * Fills given the upper and lower halves of the given rectangle 
     * in gradient style from bright to dark colors.
     */
    public static void vistaFillRectGradient(Graphics2D g, Rectangle rect,
            Color brightUpperC, Color darkUpperC, Color brightLowerC,
            Color darkLowerC) {
        vistaFillRectGradient(g, rect.x, rect.y, rect.width, rect.height,
                brightUpperC, darkUpperC, brightLowerC, darkLowerC);
    }

    /**
     * Fills given the upper and lower halves of the given rectangle 
     * in gradient style from bright to dark colors.
     */
    public static void vistaFillRectGradient(Graphics2D g, int x, int y,
            int width, int height, Color brightUpperC, Color darkUpperC,
            Color brightLowerC, Color darkLowerC) {
        paintVistaGradientFill(g, x, y, width, height / 2, brightUpperC,
                darkUpperC);
        paintVistaGradientFill(g, x, y + height / 2, width, height - height
                / 2, brightLowerC, darkLowerC);
    }

    /**
     * Fills given rectangle in gradient style from bright to dark colors,
     * the upper half of the rectangle has a single color fill.
     */
    public static void vistaFillRectGradient(Graphics2D g, Rectangle rect,
            Color upperC, Color brightLowerC, Color darkLowerC) {
        vistaFillRectGradient(g, rect.x, rect.y, rect.width, rect.height,
                upperC, brightLowerC, darkLowerC);
    }

    /**
     * Fills given rectangle in gradient style from bright to dark colors,
     * the upper half of the rectangle has a single color fill.
     */
    public static void vistaFillRectGradient(Graphics2D g, int x, int y,
            int width, int height, Color upperC, Color brightLowerC,
            Color darkLowerC) {
        g.setColor(upperC);
        g.fillRect(x, y, width, height / 2);
        paintVistaGradientFill(g, x, y + height / 2, width, height - height
                / 2, brightLowerC, darkLowerC);
    }

    /**
     * Fills given rectangle using top-down gradient fill of specified colors
     */
    private static void paintVistaGradientFill(Graphics2D g, int x, int y,
            int width, int height, Color brightC, Color darkC) {
        GradientPaint gradient = getGradientPaint(x, y, brightC, x, y
                + height, darkC);
        g.setPaint(gradient);
        g.fillRect(x, y, width, height);
    }

    public static GradientPaint getGradientPaint(float x1, float y1,
            Color upper, float x2, float y2, Color lower) {
        return getGradientPaint(x1, y1, upper, x2, y2, lower, false);
    }

    /**
     * GradientPaint creation is somewhat expensive.  This method keeps cached
     * GradientPaint instances, and normalizes the resulting GradientPaint for
     * horizontal and vertical cases.  Note that this depends entirely on the
     * hashing algorithm for accuracy - there are hypothetical situations 
     * where the return value could be wrong, though none have as yet been 
     * encountered, and given that the number of gradient heights and widths
     * actually used in any UI are very small, such a situation is highly 
     * unlikely.
     */
    public static GradientPaint getGradientPaint(float x1, float y1,
            Color upper, float x2, float y2, Color lower, boolean repeats) {
        if (noGpCache) {
            return new GradientPaint(x1, y1, upper, x2, y2, lower, repeats);
        }

        //Only for test runs with disabled customizations
        if (upper == null) {
            upper = Color.BLUE;
        }

        if (lower == null) {
            lower = Color.ORANGE;
        }

        if (gpCache == null) {
            gpCache = new HashMap<Integer, GradientPaint>(20);
        }
        //Normalize any non-repeating gradients
        boolean horizontal = x1 == x2;
        boolean vertical = y1 == y2;
        if (horizontal && vertical) {
            //Hack: gradient paint w/ 2 matching points causes endless loop 
            //in native code on mac os, so pick a random number to make sure
            //that can't happen
            y1 = x1 + 28;
        } else if (horizontal && !repeats) {
            x1 = 0;
            x2 = 0;
        } else if (vertical && !repeats) {
            y1 = 0;
            y2 = 0;
        }
        //TODO: Normalize non-planar repeating gp's by vector/relative location

        //Generate a hash code for looking up an existing paint
        long bits = Double.doubleToLongBits(x1)
                + Double.doubleToLongBits(y1) * 37
                + Double.doubleToLongBits(x2) * 43
                + Double.doubleToLongBits(y2) * 47;
        int hash = ((((int) bits) ^ ((int) (bits >> 32)))
                ^ upper.hashCode() ^ (lower.hashCode() * 17))
                * (repeats ? 31 : 1);

        Integer key = new Integer(hash);
        GradientPaint result = gpCache.get(key);
        if (result == null) {
            result = new GradientPaint(x1, y1, upper, x2, y2, lower,
                    repeats);
            if (gpCache.size() > 40) {
                gpCache.clear();
            }
            gpCache.put(key, result);
        }
        return result;
    }
}

Related Tutorials