Java Graphics Draw paintRectShadow(Graphics g, int x, int y, int width, int height)

Here you can find the source of paintRectShadow(Graphics g, int x, int y, int width, int height)

Description

Draw a shadow shaped as a Rectangle with smooth edges with a depth of 5 if possible.

License

Apache License

Parameter

Parameter Description
g a <code>Graphics</code> value

Declaration

public static void paintRectShadow(Graphics g, int x, int y, int width, int height) 

Method Source Code


//package com.java2s;
/*//  w ww  . j  a v a2 s .com
 *   Copyright 2007 skynamics AG
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import java.awt.Color;
import java.awt.Graphics;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;

public class Main {
    /** The default radius in the Sky LookAndFeel package */
    public static final int RADIUS = 4;
    /** Shadow color */
    private static final Color SHADOWCOLOR = new Color(0, 0, 0, 20);

    /**
     * Draw a shadow shaped as a Rectangle with smooth edges with a depth of 5 if possible.
     *
     * @param g a <code>Graphics</code> value
     * @nowarn
     */
    public static void paintRectShadow(Graphics g, int x, int y, int width, int height) {
        paintRectShadow(g, x, y, width, height, 5);
    }

    /**
     * Draw a shadow shaped as a Rectangle with smooth edges and a depth of shadowsteps if possible.
     *
     * @param g a <code>Graphics</code> value
     * @param shadowsteps an <code>int</code> value
     * @nowarn
     */
    public static void paintRectShadow(Graphics g, int x, int y, int width, int height, int shadowsteps) {
        Shape clip = g.getClip();

        int smooth = (int) Math.min((double) width, (double) height) / 2;
        if (smooth > shadowsteps)
            smooth = shadowsteps;

        // calculate clip for shadow
        Area area = new Area(new RoundRectangle2D.Double(x, y, width, height, RADIUS, RADIUS));

        area.subtract(new Area(new Rectangle(x, y, width - smooth, height - smooth)));

        // Be sure we don't paint in a foreign area.
        area.intersect(new Area(clip.getBounds()));

        g.setClip(area);

        g.setColor(SHADOWCOLOR);

        for (int i = 0; i < smooth; i++) {
            g.fillRoundRect(x + i, y + i, (width - (2 * i)), (height - (2 * i)), (RADIUS - i < 0) ? 0 : RADIUS - i,
                    (RADIUS - i < 0) ? 0 : RADIUS - i);
        }

        // set old clip
        g.setClip(clip);
    }
}

Related

  1. paintKnurl3(java.awt.Graphics g2, float x, float y, float width, float height)
  2. paintLine(Graphics2D g2, Point2D.Double from, Point2D.Double to)
  3. paintOval(int x, int y, Color c, int size, Graphics g)
  4. paintPoint(java.awt.Graphics g2, int x, int y)
  5. paintRectCompartment(Graphics g, Dimension size, int x, int y, Color fillColor, Color strokeColor)
  6. paintShape(Shape shape, Graphics2D g2d, Color colorStroke, Stroke stroke, Color colorFill, double downsample)
  7. paintSprite(Graphics g, int x, int y, int[][] sprite, Color[] colors)
  8. prepareGraphics(Graphics g)
  9. printAll(java.awt.Graphics2D g2, java.awt.Component component)