Java Graphics Draw Border paintBorderGlow(Graphics2D g2, int glowWidth, Shape clipShape, Color glowOuterHigh, Color glowOuterLow)

Here you can find the source of paintBorderGlow(Graphics2D g2, int glowWidth, Shape clipShape, Color glowOuterHigh, Color glowOuterLow)

Description

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

License

LGPL

Parameter

Parameter Description
g2 the <tt>Graphics</tt> object to use for painting
glowWidth the width of the glow
clipShape the shape where to paint the glow
glowOuterHigh the color which will paint the higher glow
glowOuterLow the color which will paint the lower glow

Declaration

public static void paintBorderGlow(Graphics2D g2, int glowWidth, Shape clipShape, Color glowOuterHigh,
        Color glowOuterLow) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.*;

public class Main {
    /**/*from   w ww  .j  ava  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

  1. drawSquareBorder(Graphics2D g, int x, int y, int width, int height, int rounding, Color color, float strokeWidth)
  2. paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int width, int height)
  3. paintBorder(Rectangle r, Graphics2D g2d)
  4. paintBorderDebugInfo(final Graphics g, final JComponent c, final Color color)
  5. paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth)
  6. paintSequenceMatrix(Graphics2D g2, Rectangle2D rect, String sequence, Function coloring, Function border)
  7. paintXpGradientBorder(Graphics g, int x, int y, int width, int height, Color darkC, int borderType)