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();
in.close();
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// a round rectangle.
RoundRectangle2D r = new RoundRectangle2D.Float(25, 35, 150, 150, 25,
25);
// a texture rectangle the same size as the texture image.
Rectangle2D tr = new Rectangle2D.Double(0, 0, mImage.getWidth(), mImage
.getHeight());
// the TexturePaint.
TexturePaint tp = new TexturePaint(mImage, tr);
// Now fill the round rectangle.
g2.setPaint(tp);
g2.fill(r);
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
f.getContentPane().add(new TexturePaintFill());
f.setSize(350, 250);
f.show();
}
}
|