Wednesday, August 1, 2012
Today I am going to post a program to draw partially transparent image using Graphics2D in Java. In this example I am going to paint the background first by drawing ellipses with different colors .The image is broken into tiles using BufferedImage and each tile is given a separate transparency(alpha value) and then drawn above the background. Screenshots are given below
Original Image
Transparent Image
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;

public class TransparentImage extends JPanel {
     private BufferedImage mImage;
     public static void main(String[] args) {
       try {
         String filename = "Image.jpeg";
         JFrame f = new JFrame("TransparentImage v1.0");
         TransparentImage showOff = new TransparentImage(filename);
       }catch (Exception e) {
  public TransparentImage(String filename) throws Exception{
// Get the specified image.
         InputStream in = getClass().getResourceAsStream(filename);
         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
         mImage = decoder.decodeAsBufferedImage();
// Set our size to match the image's size.
         setPreferredSize(new Dimension((int)mImage.getWidth(), (int)mImage.getHeight()));
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
// Turn on antialiasing
    private void drawBackground(Graphics2D g2) {
// Draw circles of different colors
        int side = 45;
        int width = getSize().width;
        int height = getSize().height;
        Color[] colors = { Color.yellow, Color.cyan,,
            , Color.magenta, Color.lightGray };
        for (int y = 0; y < height; y += side) {
            for (int x = 0; x < width; x += side) {
              Ellipse2D ellipse = new Ellipse2D.Float(x, y, side, side);
              int index = (x + y) / side % colors.length;
    private void drawImageTile(Graphics2D g2) {
         int side = 36;
         int width = mImage.getWidth();
         int height = mImage.getHeight();
         for (int y = 0; y < height; y += side) {
            for (int x = 0; x < width; x += side) {
// Calculate an appropriate transparency value
               float xBias = (float)x / (float)width;
               float yBias = (float)y / (float)height;
               float alpha = 1.0f - Math.abs(xBias - yBias);
AlphaComposite.SRC_OVER, alpha));
// Draw the subimage
                int w = Math.min(side, width - x);
                int h = Math.min(side, height - y);
                BufferedImage tile = mImage.getSubimage(x, y, w, h);
                g2.drawImage(tile, x, y, null);
// Reset the composite.

NOTE : in order to run this program perfectly you must have a Image.jpeg file in the same directory as that of your java file.
 Change brightness of image using RescaleOp


Post a Comment

Total Pageviews

Subscribe via Email


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.