package Response;
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
@WebServlet("/demo3")
public class ResponseTest3 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建图形对象
int width = 40;
int height = 40;
BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
//美化图片
//1.创建画笔
Graphics g = image.getGraphics();
//2.填充背景颜色
g.setColor(Color.PINK);
g.fillRect(0,0,width,height);
//3.画边框
g.setColor(Color.blue);
g.fillRect(0,0,width-1,height-1);
//4.生成随机数与字母
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//4.1 生成随机角标
Random random = new Random();
g.setColor(Color.red);
for (int i = 1 ; i <= 4 ; i++){
int index = random.nextInt(s.length());
char ch = s.charAt(index);
//写验证码
g.drawString(ch+"",width/8*i,height/2);
}
g.setColor(Color.green);
//5随机线
for (int i =0;i<10;i++){
int width1 = random.nextInt(width);
int width2 = random.nextInt(width);
int higth1 = random.nextInt(height);
int hight2 = random.nextInt(height);
g.drawLine(width1,higth1,width2,hight2);
}
//将图片输出到画面
ImageIO.write(image,"jpg",resp.getOutputStream());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}