applet绘制纹理图

 其效果如下图:
applet绘制纹理图

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;

public class BufferedImageTest extends Applet
{
	
	private static final long serialVersionUID = 1L;
	//一个BufferedImage,还有它的宽和高
	private BufferedImage image;
	private final int BI_WIDTH = 100;
	private final int BI_HEIGHT = 100;

	//用来生成随机颜色和屏幕位置
	private java.util.Random random;

	public void init()
	{
		random = new java.util.Random();

		//创建一个(BI_WIDTH*BI_HEIGHT)的缓冲图像
		image = new BufferedImage(BI_WIDTH, BI_HEIGHT,
				BufferedImage.TYPE_INT_BGR);

		//为BufferedImage创建一个Graphics2D容器。记住,它和Applet的
		//Graphics2D没有什么关系
		Graphics2D bi_g2d = image.createGraphics();

		//我们将在BufferedImage上绘制一些条纹
		//条纹的宽和高分别是图像宽和高的十分之一
		final int stripWidth = BI_WIDTH / 10;
		final int stripHeight = BI_HEIGHT / 10;

		//用随机颜色绘制垂直的条纹
		bi_g2d.setPaint(new Color(random.nextInt()));

		int x = stripWidth / 2;
		while (x < BI_WIDTH)
		{
			bi_g2d.fill(new Rectangle(x, 0, stripWidth, BI_HEIGHT));
			x += 2 * stripWidth;
		}

		//给条纹设置一个透明通道
		bi_g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
				0.7f));

		//使用随机颜色绘制水平的条纹
		bi_g2d.setPaint(new Color(random.nextInt()));
		int y = stripHeight / 2;
		while (y < BI_HEIGHT)
		{
			bi_g2d.fill(new Rectangle(0, y, BI_WIDTH, stripHeight));
			y += 2 * stripHeight;
		}

		//在图像的周围呈现一个深色不透明的外框
		bi_g2d.setStroke(new BasicStroke(2.0f));
		bi_g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
		bi_g2d.setPaint(Color.black);
		bi_g2d.draw(new Rectangle(0, 0, BI_WIDTH, BI_HEIGHT));
	}

	public void paint(Graphics g)
	{
		//将g转换,以获得一个可用的Graphics2D对象
		Graphics2D g2d = (Graphics2D) g;

		//在随机的位置上画一串图像
		for (int i = 0; i < 20; i++)
		{
			g2d.drawImage(image, AffineTransform.getTranslateInstance(random.nextInt()% getSize().getWidth(), random.nextInt()% getSize().getHeight()), this);
		}
	}
}

 

 

你可能感兴趣的:(java)