Java开发1200例第50例:纹理填充特效(源码)

来源:互联网 发布:免root长截屏软件 编辑:程序博客网 时间:2024/05/30 23:03
import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.TexturePaint;import java.awt.geom.Rectangle2D;import java.awt.geom.Rectangle2D.Float;import java.awt.image.BufferedImage;import javax.swing.JFrame;import javax.swing.JPanel;public class TextureFillFrame extends JFrame{ TextureFillPanel panel = null;  public TextureFillFrame(){  this.setTitle("纹理填充特效");  this.setBounds(100, 100, 400, 300);  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.setResizable(false);  panel = new TextureFillPanel();  this.add(panel); }  public static void main(String[] args){  TextureFillFrame frame = new TextureFillFrame();  frame.setVisible(true); } class TextureFillPanel extends JPanel{    public void paint(Graphics g){   //得到缓冲流对象   BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);   Graphics2D g2 = (Graphics2D)image.getGraphics(); //取得缓冲流的Graphics2D对象   g2.setColor(Color.BLUE);      //设置颜色   g2.fillRect(0, 0, 90, 90);    //绘制填充矩形   g2.setColor(Color.RED);       //设置颜色   g2.fillOval(95, 95, 90, 90);  //绘制填充圆形   Rectangle2D.Float rect = new Rectangle2D.Float(10, 10, 20, 20);  //创建Rectangle2D对象   TexturePaint textPaint = new TexturePaint(image, rect);          //创建纹理填充对象   Graphics2D graphics2 = (Graphics2D)g;                            //得到Graphics2D对象   graphics2.setPaint(textPaint);                                   //设置纹理填充对象   Rectangle2D.Float ellipse2 = new Rectangle2D.Float(45, 25, 200, 200);  //创建矩形对象   graphics2.fill(ellipse2);       //绘制填充纹理的矩形  } }} 注意:本文需掌握的主要是TexturePaint类的使用方法,该类的构造方法如下:TexturePaint(BufferedImage txtr,Rectangle2D anchor)txtr具有用于绘制的纹理的 BufferedImage 对象anchor用户空间中用于定位和复制纹理的 Rectangle2D