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

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

Description

Paint a 3D effect to a RoundRect.

License

Apache License

Parameter

Parameter Description
radius an <code>int</code> value

Declaration

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

Method Source Code


//package com.java2s;
/*// w w w.j av  a 2s. 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.Graphics2D;

public class Main {
    /** Maximum shadow alpha channel value */
    private static final double MAXSHADOWALPHA = 100d;
    /** Maximum light alpha channel value */
    private static final double MAXLIGHTALPHA = 100d;
    /** Helper class for shadow smoothing */
    private static Color[] shadowColorArray;
    /** Helper class for light smoothing */
    private static Color[] lightColorArray;

    /**
     * Paint a 3D effect to a RoundRect.
     *
     * @param radius an <code>int</code> value
     * @nowarn
     */
    public static void paint3DRoundRectEffect(Graphics g, int x, int y, int width, int height, int radius) {
        paint3DRoundRectEffect(g, x, y, width, height, radius, 5);
    }

    /**
     * Paint a 3D effect to a RoundRect.
     *
     * @param radius an <code>int</code> value
     * @param depth an <code>int</code> value
     * @nowarn
     */
    public static void paint3DRoundRectEffect(Graphics g, int x, int y, int width, int height, int radius,
            int depth) {
        if (shadowColorArray == null)
            createArrays();

        if (!(g instanceof Graphics2D))
            return;

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

        for (int i = 0; i < smooth; i++) {
            g.setColor(shadowColorArray[(int) (MAXSHADOWALPHA / smooth * (smooth - i)) - 1]);

            //south of the RoundRect
            g.drawLine(x + radius + 1, y + height - i, x + width - radius - 1, y + height - i);

            // area in th east of the RoundRect
            g.drawLine(x + width - i, y + radius + 1, x + width - i, y + height - radius - 1);

            // dark part of the left corner on the bottom
            g.drawArc(x + i, y + height - 2 * radius + i, 2 * (radius - i), 2 * (radius - i), -90, -45);

            // draw arc area in the right corner of the bottom
            g.drawArc(x + width - 2 * radius + i, y + height - 2 * radius + i, 2 * (radius - i), 2 * (radius - i),
                    0, -90);

            // dark part of the right corner on the top
            g.drawArc(x + width - 2 * radius + i, y + i, 2 * (radius - i), 2 * (radius - i), 45, -45);

            g.setColor(lightColorArray[(int) (MAXLIGHTALPHA / smooth * i)]);

            // area in the north of the RoundRect
            g.drawLine(x + radius + 1, y + i, x + width - radius - 1, y + i);

            //area in the west of the RoundRect
            g.drawLine(x + i, y + radius + 1, x + i, y + height - radius - 1);

            // light part of the right corner on the top
            g.drawArc(x + width - 2 * radius + i, y + i, 2 * (radius - i), 2 * (radius - i), 90, -45);

            // left corner on the top
            g.drawArc(x + i, y + i, 2 * (radius - i), 2 * (radius - i), 90, 90);

            // light part of the left corner on the bottom
            g.drawArc(x + i, y + height - 2 * radius + i, 2 * (radius - i), 2 * (radius - i), 180, 45);
        }
    }

    private static void createArrays() {
        shadowColorArray = new Color[100];
        lightColorArray = new Color[100];

        for (int i = 0; i < 100; i++) {
            shadowColorArray[i] = new Color(0, 0, 0, i);
            lightColorArray[i] = new Color(255, 255, 255, i);
        }
    }
}

Related

  1. paint(Graphics2D g)
  2. paint(Graphics2D g)
  3. paint(Graphics2D g)
  4. paint3Deffect(Graphics2D g2D, JComponent c, boolean round, boolean out)
  5. paint3DRectEffect(Graphics graphics, int x, int y, int width, int height)
  6. paintBall(Graphics2D g2, Color c)
  7. paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color)
  8. paintComponent(Graphics g, Component c, Container p, Rectangle r)
  9. paintCross(Graphics g, Color color, int centerX, int centerY, int size, int width)