GradientPaint creation is somewhat expensive. - Java 2D Graphics

Java examples for 2D Graphics:Paint

Description

GradientPaint creation is somewhat expensive.

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");

    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);
    }/*  ww  w  .  j a  va  2s  . c o m*/

    /**
     * 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