Paints border glow over the given clip Shape with the given glow high and low colors and the given glowWidth. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Paints border glow over the given clip Shape with the given glow high and low colors and the given glowWidth.

Demo Code


//package com.java2s;
import java.awt.*;

public class Main {
    /**//from w  w w.  ja v  a  2 s . c om
     * Paints border glow over the given <tt>clipShape</tt> with the given
     * glow high and low colors and the given <tt>glowWidth</tt>.
     * @param g2 the <tt>Graphics</tt> object to use for painting
     * @param glowWidth the width of the glow
     * @param clipShape the shape where to paint the glow
     * @param glowOuterHigh the color which will paint the higher glow
     * @param glowOuterLow the color which will paint the lower glow
     */
    public static void paintBorderGlow(Graphics2D g2, int glowWidth,
            Shape clipShape, Color glowOuterHigh, Color glowOuterLow) {
        int gw = glowWidth * 2;
        for (int i = gw; i >= 2; i -= 2) {
            float pct = (float) (gw - i) / (gw - 1);

            Color mixHi = getMixedColor(glowOuterHigh, pct, new Color(255,
                    255, 255, 200), 1.0f - pct);
            Color mixLo = getMixedColor(glowOuterLow, pct, new Color(255,
                    255, 255, 200), 1.0f - pct);
            g2.setPaint(new GradientPaint(0,
                    clipShape.getBounds().height * 0.25f, mixHi, 0,
                    clipShape.getBounds().height, mixLo));

            g2.setComposite(AlphaComposite.getInstance(
                    AlphaComposite.SRC_ATOP, pct));
            g2.setStroke(new BasicStroke(i));
            g2.draw(clipShape);
        }
    }

    /**
     * Returns a mixed color from color <tt>c1</tt> and <tt>c2</tt>.
     * @param c1 the start color
     * @param pct1 the first color coefficient of the mix between 0 and 1
     * @param c2 the end color
     * @param pct2 the second color coefficient of the mix between 0 and 1
     * @return the new mixed color
     */
    private static Color getMixedColor(Color c1, float pct1, Color c2,
            float pct2) {
        float[] clr1 = c1.getComponents(null);
        float[] clr2 = c2.getComponents(null);
        for (int i = 0; i < clr1.length; i++) {
            clr1[i] = (clr1[i] * pct1) + (clr2[i] * pct2);
        }
        return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
    }
}

Related Tutorials