当前位置 : 主页 > 编程语言 > c++ >

图形处理基础.java

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt package com.fz.servlet;import java.awt.BasicStroke;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Random;import java.util
gistfile1.txt
package com.fz.servlet;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import com.fz.util.RandomColor;

@WebServlet("/cherck.do") 
public class Cherck extends HttpServlet{

	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		//定义图片宽高
		int w = 200;
		int h = 200;
		//实例化缓冲图片
		BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		//获取画笔队像
		Graphics graphics = image.getGraphics();
		Graphics2D g = (Graphics2D)image.getGraphics();
		//设置颜色
		graphics.setColor(RandomColor.getColor());
		//设置形状
		graphics.fillRect(0, 0, w, h);
		//矩形
		graphics.setColor(RandomColor.getColor());
		graphics.fillRect(5, 5, w/4, h/4);
		//圆形
		graphics.setColor(RandomColor.getColor());
		graphics.fillOval(55, 55, w/4, h/4);
		//线
		graphics.setColor(RandomColor.getColor());
		graphics.drawLine(50, 50, w/2, h);
		//多条线
		Random r = new Random();
		for(int m=0;m<=20;m++){
			g.setStroke(new BasicStroke(r.nextFloat() * 20 + 1f));
			graphics.setColor(RandomColor.getColor());
			int a = r.nextInt(w);
			int b = r.nextInt(h);
			int c = r.nextInt(w);
			int d = r.nextInt(h);
			graphics.drawLine(a, b, w/2, h);
		}
		for(int m=0;m<=20;m++){
			g.setStroke(new BasicStroke(r.nextFloat() * 20 + 1f));
			g.setColor(RandomColor.getColor());
			int a = r.nextInt(w);
			int b = r.nextInt(h);
			int c = r.nextInt(w);
			int d = r.nextInt(h);
			graphics.drawLine(a, b, c, d);
		}
		//曲线
		graphics.setColor(RandomColor.getColor());
		
		int xx = r.nextInt(w-50)+50;
		int swh = r.nextInt(10)+2;
		for(int m=0;m<=w;m++){
			g.setStroke(new BasicStroke(r.nextFloat() * 20 + 1f));
			//double x =xx + Math.cos(m*Math.PI/60)*30;
			//int y = m;
			int x = m;
		    double y = (h/2) + Math.sin(m*Math.PI/200)*30;
		    graphics.fillOval((int)x, (int)y, swh,swh);
		}
		
		//文字
		graphics.setColor(RandomColor.getColor());
		graphics.drawString("乐开花呢", 60, 30);
		
		//输出(在页面显示)
		ImageIO.write(image, "png", res.getOutputStream());
		//保存图片
		/*File file = new File("c:/ma/"+UUID.randomUUID().toString()+".png");
		if (!file.exists()) {
			file.mkdirs();
		}
		ImageIO.write(image, "png", file);*/
	}

}
网友评论