Imagine that we change the triangle of figure 3.8 (Tiled Images as Fill Patterns: Example Output) with a
non polygon image, lets say a Duke image, or a Pear, or whatever you like, my question is:
In that case, could we fill that image with a pattern (like the bluedrop.gif of this TiledImages example)?
(Thanks for that tutorial link)
You can use the same method with any Shape:
http://java.sun.com/j2se/1.5/docs/api/java/awt/geom/Area.html
http://java.sun.com/j2se/1.5/docs/api/java/awt/Shape.html
Or you can set the clip and fill the entire image.
Anyway you will have to define the area to be filled using some logic of your own.
I'am not able to fill 'globalImage' with a 'patternImage'. I think setPaint has nothing to do with drawImage.
Some clues there?
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class Principal {
public static JFrame jv;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
jv = new JFrame();
jv.add(new GComp());
jv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jv.pack();
jv.setVisible(true);
jv.setSize(500, 500);
}
}
class GComp extends JComponent {
TexturePaint texture;
BufferedImage globalImg;
public GComp() {
BufferedImage patternImg = null;
try {
patternImg = ImageIO.read(new File("D://PRUEBAS//resources//patternImg.gif"));
globalImg = ImageIO.read(new File("D://PRUEBAS//resources//globalImg.gif"));
} catch (Exception e) {
e.printStackTrace();
}
texture = new TexturePaint(patternImg, new Rectangle(0, 0, 32, 32));
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(texture);
g2.drawImage(globalImg, 100, 100, null);
}
}