Java Graphics Draw drawGlow(Graphics2D g2, Area area, double width, Color color)

Here you can find the source of drawGlow(Graphics2D g2, Area area, double width, Color color)

Description

draw Glow

License

CDDL license

Declaration

public static void drawGlow(Graphics2D g2, Area area, double width, Color color) 

Method Source Code


//package com.java2s;
/*//  w  w w  .  ja va2  s . c o m
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the License). You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
 * or http://www.netbeans.org/cddl.txt.
 * 
 * When distributing Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://www.netbeans.org/cddl.txt.
 * If applicable, add the following below the CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

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

import java.awt.Graphics2D;
import java.awt.Paint;

import java.awt.Shape;
import java.awt.Stroke;

import java.awt.geom.AffineTransform;
import java.awt.geom.Area;

public class Main {
    public static void drawGlow(Graphics2D g2, Area area, double width, Color color) {
        Shape oldClip = g2.getClip();
        Stroke oldStroke = g2.getStroke();
        Composite oldComposite = g2.getComposite();
        Paint oldPaint = g2.getPaint();

        AffineTransform at = g2.getTransform();

        double pixelWidth = getScale(g2.getTransform()) * width;

        int steps = Math.min(Math.max(2, (int) Math.floor(pixelWidth / 1.4)), 8);

        Area newClip = new Area(oldClip);
        newClip.subtract(area);

        g2.setClip(newClip);
        g2.setPaint(color);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f / steps));

        width *= 2;

        for (int i = steps - 1; i >= 0; i--) {
            double t = ((double) (i + 1)) / steps;
            g2.setStroke(new BasicStroke((float) ((Math.pow(2, t) - 1) * width)));
            g2.draw(area);
        }

        g2.setComposite(oldComposite);
        g2.setStroke(oldStroke);
        g2.setClip(oldClip);
        g2.setPaint(oldPaint);
    }

    public static float getScale(AffineTransform at) {
        if (at == null)
            return 1;
        return (float) Math.hypot(at.getScaleX(), at.getShearY());
    }
}

Related

  1. drawFleche1(Graphics g, double x, double y, double x1, double y1, int L)
  2. drawFocus(Graphics g, int x, int y, int w, int h)
  3. drawFocus(Graphics2D g2, int iX, int iY, int iWidth, int iHeight)
  4. drawFrank(Graphics g, int x, int y)
  5. drawGatter(Graphics g, int xDist, int yDist)
  6. drawGroove(Graphics g, int x, int y, int w, int h, Color shadow, Color highlight)
  7. drawGroove(Graphics g, int x, int y, int width, int height, Color shadow, Color highlight)
  8. drawHandles(final Graphics g, final int[] x, final int[] y)
  9. drawIcon(Graphics g, String icon, int x, int y)