Draw a translucent image. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Draw a translucent image.

Demo Code

/*/*from   ww  w .  j  a  v  a 2s.  co m*/
 * Copyright JTheque (Baptiste Wicht)
 *
 * 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.
 */
//package com.java2s;
import java.awt.AlphaComposite;

import java.awt.Composite;

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Draw a translucent image.
     *
     * @param g2    The graphics to paint with.
     * @param image The image to paint.
     * @param x     The x position of the image.
     * @param y     The y position of the image.
     * @param alpha The alpha value of the image painting.
     */
    public static void drawAlphaImage(Graphics2D g2, BufferedImage image,
            int x, int y, float alpha) {
        drawAlphaImage(g2, image, x, y, image.getWidth(),
                image.getHeight(), alpha);
    }

    /**
     * Draw a translucent image with specified size.
     *
     * @param g2     The graphics to paint with.
     * @param image  The image to paint.
     * @param x      The x position of the image.
     * @param y      The y position of the image.
     * @param width  The width of the image.
     * @param height The height of the image.
     * @param alpha  The alpha value of the image painting.
     */
    public static void drawAlphaImage(Graphics2D g2, Image image, int x,
            int y, int width, int height, float alpha) {
        Composite oldComposite = g2.getComposite();

        g2.setComposite(AlphaComposite.SrcOver.derive(alpha));

        g2.drawImage(image, x, y, width, height, null);

        g2.setComposite(oldComposite);
    }

    /**
     * Draw a translucent image with specified source and destination rectangles.
     *
     * @param g2    The graphics to paint with.
     * @param image The image to paint.
     * @param dx1   the <i>x</i> coordinate of the first corner of the destination rectangle.
     * @param dy1   the <i>y</i> coordinate of the first corner of the destination rectangle.
     * @param dx2   the <i>x</i> coordinate of the second corner of the destination rectangle.
     * @param dy2   the <i>y</i> coordinate of the second corner of the destination rectangle.
     * @param sx1   the <i>x</i> coordinate of the first corner of the source rectangle.
     * @param sy1   the <i>y</i> coordinate of the first corner of the source rectangle.
     * @param sx2   the <i>x</i> coordinate of the second corner of the source rectangle.
     * @param sy2   the <i>y</i> coordinate of the second corner of the source rectangle.
     * @param alpha The alpha value of the image painting.
     */
    public static void drawAlphaImage(Graphics2D g2, Image image, int dx1,
            int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
            float alpha) {
        Composite oldComposite = g2.getComposite();

        g2.setComposite(AlphaComposite.SrcOver.derive(alpha));

        g2.drawImage(image, //Image
                dx1, dy1, dx2, dy2, //Destination
                sx1, sy1, sx2, sy2, null); //Source

        g2.setComposite(oldComposite);
    }
}

Related Tutorials