package cn.itcast.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
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;
public class Chackcode extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int width = 120;
int height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.GRAY);
g.fillRect(0,0,width,height);
g.setColor(Color.BLUE);
g.drawRect(0, 0, width-1, height-1);
String words = "QWERTYUIOPLKJHGFDASZXCVBBMNMqwertyuioplkjhgfdsazxcvbnm123456789";
Random random = new Random();
g.setColor(Color.BLACK);
int x = 20;
int y = 20;
int x1, x2, y1, y2;
g.setFont(new Font("隶书",Font.BOLD,20));
for(int i=0;i<4;i++)
{
int jiaodu = random.nextInt(90)-45;
double hudu = jiaodu*Math.PI/180;
g.rotate(hudu, x, y);
int index = random.nextInt(words.length());
char ch = words.charAt(index);
g.drawString(""+ch, x, y);
g.rotate(-hudu, x, y);
x+=20;
x1 = random.nextInt(width);
y1 = random.nextInt(height);
x2 = random.nextInt(width);
y2 = random.nextInt(height);
g.drawLine( x1, y1, x2, y2);
}
ImageIO.write(image, "jpg",response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}