Java Draw Shadow drawShadow(Graphics2D g2d, Color topColor, float opacity, int shadowWidth, int cornerRadius, int x, int y, int width, int height)

Here you can find the source of drawShadow(Graphics2D g2d, Color topColor, float opacity, int shadowWidth, int cornerRadius, int x, int y, int width, int height)

Description

draw Shadow

License

Apache License

Declaration

public static void drawShadow(Graphics2D g2d, Color topColor, float opacity, int shadowWidth, int cornerRadius,
            int x, int y, int width, int height) 

Method Source Code


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

import java.awt.*;

import java.awt.geom.RoundRectangle2D;

public class Main {
    public static void drawShadow(Graphics2D g2d, Color topColor, float opacity, int shadowWidth, int cornerRadius,
            int x, int y, int width, int height) {
        if (shadowWidth == 0) {
            return;
        }/*from  www  .j  a v a  2 s. c  o  m*/
        g2d.setColor(new Color(topColor.getRed(), topColor.getGreen(), topColor.getBlue(),
                ((int) (opacity * 255)) & 0xFF));
        float alpha = 0.1f;
        float step = (float) (0.9 / shadowWidth);
        for (int i = 1; i <= shadowWidth; i++) {
            if (alpha >= 0.9f) {
                alpha = 1f;
            } else {
                alpha += step;
            }
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            RoundRectangle2D shadowShape = new RoundRectangle2D.Float(x + i, y + i, width - (i * 2),
                    height - (i * 2), cornerRadius, cornerRadius);
            g2d.draw(shadowShape);
        }
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
    }
}

Related

  1. drawShadedLine(java.awt.Graphics2D g2, float x0, float y0, float x1, float y1, java.awt.Paint topLeftPaint, java.awt.Paint otherPaint, java.awt.Paint bottomRightPaint, java.awt.geom.GeneralPath buffer)
  2. drawShadowedShape(Shape shape, Graphics2D g2d)
  3. drawShadowedString(Graphics2D g2d, String label, Color textColor, Color shadowColor, int x, int y)
  4. drawShadowedString(String string, int x, int y, Graphics2D g2d)