Monday, January 21, 2013
Today I am going to post a program that will use a text string as a clipping path and draw an image with it. As you know, Graphics2D can do a number of operations like transformations,clipping and alpha compositing. Here in our case, we will use images. First of all we will rotate the user space. This is just to give our output a more better look. The clipping shape is created in the method getClippingShape(Graphics) method. Here a text is chosen and a Font instance is created. This font is used to create GlyphVector for the clipping shape string. Then the shape is retrieved by calling getOutline() method of GlyphVector. The clip method of Graphics2D is called, followed by reading of the image from file using ImageIO's read method. Finally the image is drawn on panel and displayed.
Screenshots of original and final images
Origibal Image
Final Clip Image








--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------

import java.awt.*; 
import java.awt.font.*; 
import java.awt.image.BufferedImage; 
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.imageio.ImageIO; 

public class ClipImage extends JPanel{

   @Override
    public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;  //casting
        Dimension d = getSize();
        g2.rotate(-Math.PI / 12, d.width / 2, d.height / 2);  //rotating
        g2.clip(getClippingShape(g2));  //clipping
        try{
        //reading image from file
           final BufferedImage image = ImageIO.read(new File("image.jpg"));
           g2.drawImage(image, 0, 0, null);  //drawing image
        }catch(IOException e){
        e.printStackTrace();
        }
    }

    private Shape getClippingShape(Graphics2D g2) {
          String s = "Java 2D";  //defining clipping text
          Font font = new Font("Serif", Font.PLAIN, 122);  //text font
          FontRenderContext frc = g2.getFontRenderContext(); 
          GlyphVector gv = font.createGlyphVector(frc, s);  //text glyph
          Shape clippingShape = gv.getOutline(10, 120);  //getting clip shape
          return clippingShape; //returning shape
     }
     
     public static void main(String[] args){ 
        JFrame f=new JFrame("Clipping_Image");
        f.getContentPane().add(new ClipImage());
        f.setSize(430,220);
        f.setVisible(true);
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
     }
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters