create BufferedImage Reflection - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

create BufferedImage Reflection

Demo Code

/**/*w ww.  j a v  a  2 s. c o  m*/
 *   Copyright 2012 Dr. Krusche & Partner PartG
 *
 *   AMES-Web-Service is free software: you can redistribute it and/or 
 *   modify it under the terms of the GNU General Public License 
 *   as published by the Free Software Foundation, either version 3 of 
 *   the License, or (at your option) any later version.
 *
 *   AMES- Web-Service is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * 
 *  See the GNU General Public License for more details. 
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this software. If not, see <http://www.gnu.org/licenses/>.
 *
 */
//package com.java2s;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * @param image
     * @return
     * @throws Exception
     */
    public static BufferedImage createReflection(BufferedImage image)
            throws Exception {

        double hfact = 0.5;

        /* 
         * Dimension of original image (e.g. 96 x 96)
         */
        int width = image.getWidth(null);
        int height = image.getHeight(null);

        /* 
         * Result is the final image including the reflection
         */
        BufferedImage result = new BufferedImage(width, new Double(height
                * (1 + hfact)).intValue(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = result.createGraphics();

        /* 
         * Paint original image onto graphics
         */
        g.drawImage(image, 0, 0, null);

        /* 
         * Paint mirrored image
         */
        g.scale(1.0, -1.0);
        g.drawImage(image, 0, -image.getHeight() * 2, null);
        g.scale(1.0, -1.0);

        /* 
         * Move to mirror's origin
         */
        g.translate(0, image.getHeight());

        /* 
         * Create gradient mask
         */
        GradientPaint mask = new GradientPaint(0, 0, new Color(1f, 1f, 1f,
                0.5f), 0, image.getHeight() / 2, new Color(1f, 1f, 1f, 0f));
        g.setPaint(mask);

        /* 
         * Set alpha composite
         */
        g.setComposite(AlphaComposite.DstIn);

        /* 
         * Paint the mask
         */
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.dispose();

        return result;

    }
}

Related Tutorials