Working With TexturePaint in Java

Introduction

 
In this article, we are going to describe how to give your text a Texture style. In the Java language it is a separate class, found in the AWT package. A texture is a bitmap image applied to the surface in computer graphics. We can also fill our graphics shapes with textures. A texture is a bitmap image applied to the surface in computer graphics. We can also fill our graphics shapes with textures. The size of the BufferedImage object should be small because the BufferedImage data is copied by the TexturePaint object. At construction time, the texture is anchored to the upper left corner of a Rectangle2D that is specified in user space. Texture is computed for locations in the device space by conceptually replicating the specified Rectangle2D infinitely in all directions in user space and mapping the BufferedImage to each replicated Rectangle2D.
 

Constructor Details

 
TexturePaint(BufferedImage txtr, Rectangle2D anchor)
 
In this class its only a single constructor that takes two arguments; the first argument is an object of the buffered image with the texture to be used for painting; the second argument is an anchor in the Rectangle2D in the user space used to anchor and replicate the texture.
 

Method Details

 
1. getImage():  This method returns the BufferedImage texture used to fill the shapes. And it's syntax is: public BufferedImage getImage().
 
2. getAnchorRect(): This method is used to return a copy of the anchor rectangle which positions and sizes the textured image.  And its syntax is: public Rectangle2D getAnchorRect().
 
3. getTransparency(): This method is used for returning the transparency mode for this TexturePaint. And its syntax is: public int getTransparency().
 
4. createContext(): This method is used to generate a color pattern. It creates and returns a context used to generate the color pattern. Its syntax is public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) and its parameter cm is for the ColorModel that receives the Paint data. This is used only as a hint. The second parameter deviceBounds is for the device space bounding box of the graphics primitive being rendered. The third argument is userBounds for the user space bounding box of the graphics primitive being rendered. And the last argument is a hint; a RenderingHints object that can be used to specify how the pattern is ultimately rendered.
 
Example
  1. import java.awt.Color;  
  2. import java.awt.Container;  
  3. import java.awt.Font;  
  4. import java.awt.Graphics;  
  5. import java.awt.Graphics2D;  
  6. import java.awt.Rectangle;  
  7. import java.awt.RenderingHints;  
  8. import java.awt.TexturePaint;  
  9. import java.awt.image.BufferedImage;  
  10. import javax.swing.JComponent;  
  11. import javax.swing.JFrame;  
  12. public class TexurePaint {  
  13.     public static void main(String[] args) {  
  14.         JFrame jf = new JFrame("TexurePaint(by dubey)");  
  15.         Container cp = jf.getContentPane();  
  16.         MyCanvas tl = new MyCanvas();  
  17.         cp.add(tl);  
  18.         jf.setSize(400200);  
  19.         jf.setVisible(true);  
  20.     }  
  21. }  
  22. class MyCanvas extends JComponent {  
  23.     public void paint(Graphics g) {  
  24.         BufferedImage bim;  
  25.         TexturePaint tp;  
  26.         String mesg = "ABHISHEK ";  
  27.         Font myFont = new Font("Bodoni MT Black", Font.BOLD, 72);  
  28.         Color[] colors = {  
  29.             Color.red,  
  30.             Color.green,  
  31.             Color.blue,  
  32.         };  
  33.         int width = 25, height = 25;  
  34.         bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
  35.         Graphics2D g2 = bim.createGraphics();  
  36.         for (int i = 0; i < width; i++) {  
  37.             g2.setPaint(colors[(i / 3) % colors.length]);  
  38.             g2.drawLine(0, i, i, 0);  
  39.             g2.drawLine(width - i, height, width, height - i);  
  40.         }  
  41.         Rectangle r = new Rectangle(00, bim.getWidth(), bim.getHeight());  
  42.         tp = new TexturePaint(bim, r);  
  43.         Graphics2D g2d = (Graphics2D) g;  
  44.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  45.             RenderingHints.VALUE_ANTIALIAS_ON);  
  46.         g2d.setPaint(tp);  
  47.         g2d.setFont(myFont);  
  48.         g2d.drawString(mesg, 10100);  
  49.     }  
  50. }  
OUTPUT
 
my2.gif
 
my1.gif