Paints a rectangular drop shadow with a 5 pixel offset. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Paints a rectangular drop shadow with a 5 pixel offset.

Demo Code


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

public class Main {
    /**//w w w .java2s .  c  om
     * Paints a rectangular drop shadow with a 5 pixel offset.  The bounds passed
     * into the function should be the actual area that is to be shadowed.  The
     * function calculates the shadow's coordinates itself.
     * @param g the Graphics instance
     * @param x the x position of the component
     * @param y the y position of the component
     * @param width the width of the component
     * @param height the height of the component
     */
    public static void paintDropShadow(Graphics g, int x, int y, int width,
            int height) {
        paintDropShadow(g, x, y, width, height, 5);
    }

    /**
     * Paints a rectangular drop shadow with the specified pixel offset.  The bounds passed
     * into the function should be the actual area that is to be shadowed.  The
     * function calculates the shadow's coordinates itself.
     * @param g the Graphics instance
     * @param x the x position of the component
     * @param y the y position of the component
     * @param width the width of the component
     * @param height the height of the component
     * @param offset the offset of the shadow in pixels
     */
    public static void paintDropShadow(Graphics g, int x, int y, int width,
            int height, int offset) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        int xOff = x + offset;
        int yOff = y + offset;
        g2.setColor(new Color(45, 45, 45, 80));
        g2.fillRoundRect(xOff, yOff, width, height, offset, offset);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_OFF);
    }
}

Related Tutorials