TexturePaint

TexturePaintDemo Example: (Run Applet)

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class TexturePaintDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/smiley.jpg")).getImage();
            final Graphics2D g2 = (Graphics2D) g;
            final int textureWidth = 30;
            final int textureHeight = 30;
            final BufferedImage textureImg = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_INT_RGB);
            final Graphics2D textureG = textureImg.createGraphics();
            textureG.drawImage(image, 0, 0, textureWidth, textureHeight, this);

            final Rectangle rectangle = new Rectangle(0, 0, textureWidth, textureHeight);
            final TexturePaint texturePaint = new TexturePaint(textureImg, rectangle);

            g2.setPaint(texturePaint);
            g2.fillOval(0, 0, getWidth(), getHeight());
        }
    }
}

Texture Painting Text

TexturePaint_Text_Demo Example: (Run Applet)

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class TexturePaint_Text_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/smiley.jpg")).getImage();
            final Graphics2D g2 = (Graphics2D) g;
            final int textureWidth = 30;
            final int textureHeight = 30;
            final BufferedImage textureImg = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_INT_RGB);
            final Graphics2D textureG = textureImg.createGraphics();
            textureG.drawImage(image, 0, 0, textureWidth, textureHeight, this);

            final Rectangle rectangle = new Rectangle(0, 0, textureWidth, textureHeight);
            final TexturePaint texturePaint = new TexturePaint(textureImg, rectangle);

            g2.setPaint(texturePaint);

            g2.setFont(new Font("Times Roman", Font.BOLD + Font.ITALIC, 140));
            g2.drawString("DkIT", 0, 150);
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>