fill Blurred Border Rect - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

fill Blurred Border Rect

Demo Code


//package com.java2s;
import java.awt.Color;
import java.awt.Graphics;

public class Main {
    public static void fillBlurredBorderRect(Graphics g, Color c, int x,
            int y, int width, int height, int horizBlurSize,
            int vertBlurSize) {
        Color previousColor = g.getColor();
        g.setColor(c);//from  w w  w . j  a  v  a  2s .  c om
        g.fillRect(x, y, width, height);

        if (vertBlurSize > 0) {
            int vertAlphaStep = previousColor.getAlpha() / vertBlurSize;
            for (int i = 1; i <= vertBlurSize; ++i) {
                g.setColor(setColorAlpha(previousColor, g.getColor()
                        .getAlpha() - vertAlphaStep));
                g.drawLine(x, y - i, x + width - 1, y - i);
                g.drawLine(x, y + height + i - 1, x + width - 1, y + height
                        + i - 1);
            }
        }

        if (horizBlurSize > 0) {
            g.setColor(previousColor);
            int vertAlphaStep = previousColor.getAlpha() / vertBlurSize;
            for (int i = 1; i <= horizBlurSize; ++i) {
                g.setColor(setColorAlpha(previousColor, g.getColor()
                        .getAlpha() - vertAlphaStep));
                g.drawLine(x - i, y, x - i, y + height - 1);
                g.drawLine(x + width + i - 1, y, x + width + i - 1, y
                        + height - 1);
            }
        }

    }

    public static Color setColorAlpha(Color c, int alpha) {
        if (alpha > 255 || alpha < 0)
            return c;
        return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
    }
}

Related Tutorials