Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
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()); } } }
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); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.