Java Graphics Draw paint3DRectEffect(Graphics graphics, int x, int y, int width, int height)

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

Description

Paint a 3D effect to a Rectangle

License

Apache License

Parameter

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

Declaration

public static void paint3DRectEffect(Graphics graphics, int x, int y, int width, int height) 

Method Source Code


//package com.java2s;
/*/*from ww w .java  2 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.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 Rectangle
     *
     * @param graphics a <code>Graphics</code> value
     * @nowarn
     */
    public static void paint3DRectEffect(Graphics graphics, int x, int y, int width, int height) {
        paint3DRectEffect(graphics, x, y, width, height, 5);
    }

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

        if (!(graphics instanceof Graphics2D))
            return;

        Graphics2D g = (Graphics2D) graphics;

        int smooth = (int) Math.min((double) width, (double) 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 Rectangle
            g.drawLine(x + i + 1, y + height - i, x + width - i - 1, y + height - i);

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

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

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

            //area in the west of the Rectangle
            g.drawLine(x + i, y + i, x + i, y + height - i);
        }
    }

    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. getUsableScreenBounds(GraphicsConfiguration gconf)
  2. paint(Graphics2D g)
  3. paint(Graphics2D g)
  4. paint(Graphics2D g)
  5. paint3Deffect(Graphics2D g2D, JComponent c, boolean round, boolean out)
  6. paint3DRoundRectEffect(Graphics g, int x, int y, int width, int height, int radius)
  7. paintBall(Graphics2D g2, Color c)
  8. paintColorImageFromAlphaImage(Image alphaImage, Image out, Color color)
  9. paintComponent(Graphics g, Component c, Container p, Rectangle r)