TexturePaint with GIF image : TexturePaint « 2D Graphics « Java Tutorial






import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

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

public class MainClass extends JFrame {
  public MainClass() {
    getContentPane().add(new DrawingCanvas());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setVisible(true);
  }

  public static void main(String arg[]) {
    new MainClass();
  }
}

class DrawingCanvas extends JPanel {
  Font font = new Font("Dialog", Font.BOLD, 40);
  FontMetrics fontMetrics;

  DrawingCanvas() {
    setSize(300, 300);
    setBackground(Color.white);
    fontMetrics = getFontMetrics(font);
  }

  public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Image displayImage = getToolkit().getImage("c:\your.gif");
    
    if (displayImage != null) {
      BufferedImage bi = new BufferedImage(
                         displayImage.getWidth(this),
                         displayImage.getHeight(this),
                         BufferedImage.TYPE_INT_RGB);
      bi.createGraphics().drawImage(displayImage, 0, 0, this);

      Rectangle2D rectangle = new Rectangle2D.Float(0, 0,
                                 displayImage.getWidth(this),
                                 displayImage.getHeight(this));

      TexturePaint tp = new TexturePaint(bi, rectangle);
      g2D.setPaint(tp);
      g2D.fill(new Rectangle2D.Float(0, 0, getWidth(), getHeight()));
  }

  }
}








16.19.TexturePaint
16.19.1.A texture is a bitmap image applied to a shape
16.19.2.TexturePaint with GIF image
16.19.3.TexturePaint with BufferedImageTexturePaint with BufferedImage