16. 19. 1. 纹理用法 - A texture is a bitmap image applied to a shape

 16. 19. 1. 纹理用法 - A texture is a bitmap image applied to a shape_第1张图片

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Textures extends JPanel{
	BufferedImage s;
	
	//ImageIO:该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法
	public Textures(){
		try{
			s = ImageIO.read(this.getClass().getResource("mm.jpg"));//从类目录读取
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
	    Graphics2D g2d = (Graphics2D) g;

	    /**
	     * TexturePaint 类提供一种用被指定为 BufferedImage 的纹理填充 Shape 的方式。
	     * 因为 BufferedImage 数据由 TexturePaint 对象复制,所以 BufferedImage 对象的大小应该小一些。
	     * 在构造时,纹理定位在用户空间中指定的 Rectangle2D 的左上角。
	     * 在理论上,计算纹理在设备空间中的位置的方式是,
	     * 在用户空间中的所有方向上无限制地复制指定 Rectangle2D,
	     * 然后将 BufferedImage 映射到各个复制的 Rectangle2D。
	     */
	    TexturePaint slatetp = new TexturePaint(s, new Rectangle(0,0,60,100));
	    g2d.setPaint(slatetp);
	    g2d.fillRect(10, 5, 280, 280);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Textures");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.add(new Textures());
	    frame.setSize(280, 280);
	    frame.setVisible(true);
	}

}


 

你可能感兴趣的:(16. 19. 1. 纹理用法 - A texture is a bitmap image applied to a shape)