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

import java.awt.Color;

import java.awt.Container;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.RenderingHints;

import java.awt.TexturePaint;

import java.awt.image.BufferedImage;

 

import javax.swing.JComponent;

import javax.swing.JFrame;

 

public class TexurePaint {

      public static void main(String[] args) {

            JFrame jf = new JFrame("TexurePaint(by dubey)");

            Container cp = jf.getContentPane();

            MyCanvas tl = new MyCanvas();

            cp.add(tl);

            jf.setSize(400, 200);

            jf.setVisible(true);

      }

}

 

class MyCanvas extends JComponent {

 

      public void paint(Graphics g) {

            BufferedImage bim;

 

            TexturePaint tp;

 

            String mesg = "ABHISHEK ";

 

            Font myFont = new Font("Bodoni MT Black", Font.BOLD, 72);

 

            Color[] colors = { Color.red, Color.green, Color.blue, };

 

            int width = 25, height = 25;

            bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

            Graphics2D g2 = bim.createGraphics();

            for (int i = 0; i < width; i++) {

                  g2.setPaint(colors[(i / 3) % colors.length]);

                  g2.drawLine(0, i, i, 0);

                  g2.drawLine(width - i, height, width, height - i);

            }

            Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());

            tp = new TexturePaint(bim, r);

 

            Graphics2D g2d = (Graphics2D) g;

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

                        RenderingHints.VALUE_ANTIALIAS_ON);

            g2d.setPaint(tp);

            g2d.setFont(myFont);

            g2d.drawString(mesg, 10, 100);

      }

}

OUTPUT

my2.gif

my1.gif

Up Next
    Ebook Download
    View all
    Learn
    View all