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
--------------------------------------------------------------------------------------------------------------------------
 SOURCE CODE
--------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
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);
         f.getContentPane().add(showOff);
         f.setSize(f.getPreferredSize());
         f.setResizable(false);
         f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
         f.setVisible(true);
       }catch (Exception e) {
           System.out.println(e);
           System.exit(0);
       }
  }
  public TransparentImage(String filename) throws Exception{
        
// Get the specified image.
         InputStream in = getClass().getResourceAsStream(filename);
         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
         mImage = decoder.decodeAsBufferedImage();
         in.close();
        
// Set our size to match the image's size.
         setPreferredSize(new Dimension((int)mImage.getWidth(), (int)mImage.getHeight()));
    }
   
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
       
// Turn on antialiasing
 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        drawBackground(g2);
        drawImageTile(g2);
    }
   
    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.orange,
                      Color.pink, 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;
              g2.setPaint(colors[index]);
              g2.fill(ellipse);
            }
        }
    }
    
    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);
               g2.setComposite(AlphaComposite.getInstance(
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.
 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
     }
}

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.
--------------------------------------------------------------------------------------------------------------------------
RELATED POSTS
--------------------------------------------------------------------------------------------------------------------------
 Change brightness of image using RescaleOp

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters