Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Graphics2D Show Textured Paint

Related Tutorials


 
 

Share on Google+Share on Google+

Show Textured Paint

Advertisement
Texture refers to the roughness of the smoothness of a work of art. The way in which you paint involves texture.

Show Textured Paint

     

This section shows you the textured paint.

Texture refers to the roughness of the smoothness of a work of art. The way in which you paint involves texture. We are providing you an example that will show the textured paint.

The class TexturePaint provides an easy and impressive way to fill a shape with a repeating pattern. When you create a TexturePaint, you specify a BufferedImage to use as the pattern. The method createGraphics() creates a Graphics2D to draw into this BufferedImage.

To draw the oval we have used Ellipse2D class. To draw rectangle inside the oval, class Rectangle2D is used. The method g2d.fill(oval) fills the shape specified.

Here is the code of ShowTexturePaint.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;

public class ShowTexturePaint extends JPanel {
 
  public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  BufferedImage bufferedImage = new BufferedImage(88,
  BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = bufferedImage.createGraphics();
  g2.setColor(Color.red);
  g2.fillRect(0088);
  g2.setColor(Color.gray);
  g2.fillOval(0088);
  Rectangle2D rect = new Rectangle2D.Double(5588);
  g2d.setPaint(new TexturePaint(bufferedImage, rect));
  Ellipse2D oval = new  Ellipse2D.Double(8,8,250,250);
  g2d.fill(oval);
  }
  public static void main(String args[]) {
  JFrame frame = new JFrame("Show Texture Paint");
  ShowTexturePaint texturePaint = new ShowTexturePaint();
  frame.getContentPane().add("Center", texturePaint);
  frame.setSize(350300);
  frame.setVisible(true);
  }
}

Output will be displayed as:

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 16, 2008

Related Tutorials

Discuss: Show Textured Paint  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com