Java Graphics Draw Border paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth)

Here you can find the source of paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth)

Description

paint Border Glow

License

Open Source License

Parameter

Parameter Description
g2 a parameter
shape a parameter
glowWidth a parameter

Declaration

public static void paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth) 

Method Source Code


//package com.java2s;
/* Copyright (C) 2015, University of Kansas Center for Research
 * //from w w w . j a va 2  s.c o  m
 * Specify Software Project, specify@ku.edu, Biodiversity Institute,
 * 1345 Jayhawk Boulevard, Lawrence, Kansas, 66045, USA
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;

import java.awt.GradientPaint;
import java.awt.Graphics2D;

import java.awt.Shape;

public class Main {
    private static final Color clrGlowInnerHi = new Color(253, 239, 175, 148);
    private static final Color clrGlowInnerLo = new Color(255, 209, 0);
    private static final Color clrGlowOuterHi = new Color(253, 239, 175, 124);
    private static final Color clrGlowOuterLo = new Color(255, 179, 0);

    /**
     * @param g2
     * @param shape
     * @param glowWidth
     */
    public static void paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth) {
        int gw = glowWidth * 2;
        for (int i = gw; i >= 2; i -= 2) {
            float pct = (float) (gw - i) / (gw - 1);

            Color mixHi = getMixedColor(clrGlowInnerHi, pct, clrGlowOuterHi, 1.0f - pct);
            Color mixLo = getMixedColor(clrGlowInnerLo, pct, clrGlowOuterLo, 1.0f - pct);
            g2.setPaint(new GradientPaint(0.0f, 40 * 0.25f, mixHi, 0.0f, 40, mixLo));

            g2.setColor(Color.WHITE);

            // See my "Java 2D Trickery: Soft Clipping" entry for more
            // on why we use SRC_ATOP here
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, pct));
            g2.setStroke(new BasicStroke(i));
            g2.draw(shape);
        }
    }

    /**
     * @param c1
     * @param pct1
     * @param c2
     * @param pct2
     * @return
     */
    public 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. drawHighlightBorder(Graphics g, int x, int y, int width, int height, boolean raised, Color shadow, Color highlight)
  2. drawSquareBorder(Graphics2D g, int x, int y, int width, int height, int rounding, Color color, float strokeWidth)
  3. paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int width, int height)
  4. paintBorder(Rectangle r, Graphics2D g2d)
  5. paintBorderDebugInfo(final Graphics g, final JComponent c, final Color color)
  6. paintBorderGlow(Graphics2D g2, int glowWidth, Shape clipShape, Color glowOuterHigh, Color glowOuterLow)
  7. paintSequenceMatrix(Graphics2D g2, Rectangle2D rect, String sequence, Function coloring, Function border)
  8. paintXpGradientBorder(Graphics g, int x, int y, int width, int height, Color darkC, int borderType)