public class valist extends HttpServlet {
//字体
private final Font font = new Font("微软雅黑",Font.PLAIN,15);
private final int lineWidth = 2;
private final int width = 90;
private final int height = 25;
//图片需要的障碍点
private final int count = 200;
//字体所循环输出不同的颜色
private Color getRandColor(int fc,int bc){
final Random random = new Random();
if(fc > 255){
fc = 255;
}
if(bc > 255){
bc = 255;
}
final int r = fc + random.nextInt(bc-fc);
final int g = fc + random.nextInt(bc-fc);
final int b = fc + random.nextInt(bc-fc);
return new Color(r,g,b);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//循环输出图片的障碍小点
for(int i = 0; i<count; i++){
g.setColor(getRandColor(150,200));
final int x = random.nextInt(width - lineWidth) + 1;
final int y = random.nextInt(height - lineWidth) + 1;
final int xl = random.nextInt(lineWidth);
final int yl = random.nextInt(lineWidth);
g.drawLine(x, y, x + xl, y+yl);
}
//图片随机数字
String sRand = "";
for(int i = 0; i < 4;i++){
final String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(130),
20 +random.nextInt(130),20 + random.nextInt(130)));
g.drawString(rand, (13 * i) + 6,16);
}