Texture paint : Gradient Paint « 2D Graphics GUI « Java

Texture paint

Texture paint

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

public class TexturePaintFill extends JPanel {
  private BufferedImage mImage;

  public TexturePaintFill() throws IOException, ImageFormatException {
    // Load the specified JPEG file.
    InputStream in = getClass().getResourceAsStream("largeJava2sLogo.jpg");
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
    mImage = decoder.decodeAsBufferedImage();

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    // a round rectangle.
    RoundRectangle2D r = new RoundRectangle2D.Float(25, 35, 150, 150, 25,
    // a texture rectangle the same size as the texture image.
    Rectangle2D tr = new Rectangle2D.Double(0, 0, mImage.getWidth(), mImage
    // the TexturePaint.
    TexturePaint tp = new TexturePaint(mImage, tr);
    // Now fill the round rectangle.

  public static void main(String[] args) throws Exception {
    JFrame f = new JFrame();
    f.getContentPane().add(new TexturePaintFill());
    f.setSize(350, 250);



Related examples in the same category

1.Gradients: a smooth blending of shades from light to dark or from one color to another
2.Gradient Shapes
3.GradientPaint demoGradientPaint demo
4.GradientPaint EllipseGradientPaint Ellipse
5.Another GradientPaint DemoAnother GradientPaint Demo
6.Text effect: rotation and transparentText effect: rotation and transparent
7.Text effect: image texture
8.Round GradientPaint Fill demoRound GradientPaint Fill demo
9.GradientPaint: ironGradientPaint: iron
10.Color gradientColor gradient
11.Drawing with a Gradient Color
12.A non-cyclic gradient
13.A cyclic gradient
15.Horizontal Gradients
16.Vertical Gradient Paint
17.Gradients in the middle
18.Control the direction of Gradients
19.Returns true if the two Paint objects are equal OR both null.
20.Gradient effects