load Translucent Image - Java 2D Graphics

Java examples for 2D Graphics:Image Load

Description

load Translucent Image

Demo Code


//package com.java2s;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;

public class Main {
    public static BufferedImage loadTranslucentImage(String ref,
            float transperancy) {
        // Load the image   
        BufferedImage loaded = loadImage(ref);

        // Create the image using the    
        BufferedImage aimg = new BufferedImage(loaded.getWidth(),
                loaded.getHeight(), BufferedImage.TRANSLUCENT);
        // Get the images graphics   
        Graphics2D g = aimg.createGraphics();
        // Set the Graphics composite to Alpha   
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                transperancy));/*from   w  ww .j  a v  a2 s  .  c  o m*/
        // Draw the LOADED img into the prepared reciver image   
        g.drawImage(loaded, null, 0, 0);
        // let go of all system resources in this Graphics   
        g.dispose();
        // Return the image   
        return aimg;
    }

    public static BufferedImage loadImage(String ref) {
        BufferedImage bimg = null;
        try {

            bimg = ImageIO.read(new File(ref));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bimg;
    }
}

Related Tutorials