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
--------------------------------------------------------------------------------------------------------------------------
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 the source from Mediafire
DOWNLOAD the source from 4shared
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
Labels:Graphics2D,Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment