标签:style blog http java color width
1 package cn.itcast.response; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.image.BufferedImage; 8 import java.io.FileInputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 import java.io.PrintWriter; 13 import java.util.Random; 14 15 import javax.imageio.ImageIO; 16 import javax.servlet.ServletException; 17 import javax.servlet.http.HttpServlet; 18 import javax.servlet.http.HttpServletRequest; 19 import javax.servlet.http.HttpServletResponse; 20 21 public class ResponseDemo extends HttpServlet { 22 23 public static final int WIDTH = 120; 24 public static final int HEIGHT = 45; 25 26 public void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 29 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, 30 BufferedImage.TYPE_INT_RGB); 31 32 Graphics g = image.getGraphics(); 33 34 // 1.设置背景色 35 setBackGround(g); 36 37 // 2.设置边框 38 setBorder(g); 39 40 // 3.画干扰线 41 drawRandomLine(g); 42 43 // 4.写随机数 44 drawRandomNum((Graphics2D) g); 45 46 // 5.图形写给浏览器 47 response.setContentType("image/jpeg"); 48 49 //发头控制浏览器不要缓存 50 response.setDateHeader("expries", -1); 51 response.setHeader("Cache-Control", "no-cache"); 52 response.setHeader("Pragma", "no-cache"); 53 ImageIO.write(image, "jpg", response.getOutputStream()); 54 } 55 56 private void drawRandomNum(Graphics2D g) { 57 58 g.setColor(Color.RED); 59 g.setFont(new Font("宋体",Font.BOLD,20)); 60 61 String base = "\u7684\u4e00\u662f\u6211\u4e0d\u5728\u4eba"; 62 63 int x=5; 64 for(int i=0;i<4;i++){ 65 66 int degree = new Random().nextInt()%30; 67 68 String ch = base.charAt(new Random().nextInt(base.length()))+""; 69 70 g.rotate(degree*Math.PI/180,x,20);//设置旋转角度 71 g.drawString(ch, x, 20); 72 g.rotate(-degree*Math.PI/180,x,20); 73 x+=30; 74 } 75 76 } 77 78 private void drawRandomLine(Graphics g) { 79 g.setColor(Color.GREEN); 80 for (int i = 0; i < 5; i++) { 81 int x1 = new Random().nextInt(WIDTH); 82 int y1 = new Random().nextInt(HEIGHT); 83 84 int x2 = new Random().nextInt(WIDTH); 85 int y2 = new Random().nextInt(HEIGHT); 86 87 g.drawLine(x1, y1, x2, y2); 88 } 89 } 90 91 private void setBorder(Graphics g) { 92 93 g.setColor(Color.BLUE); 94 g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2); 95 96 } 97 98 private void setBackGround(Graphics g) { 99 100 g.setColor(Color.WHITE); 101 g.fillRect(0, 0, WIDTH, HEIGHT); 102 103 } 104 105 public void doPost(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException, IOException { 107 108 } 109 110 }
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <title>register.html</title> 5 6 </head> 7 <script type="text/javascript"> 8 function changeImage(img){ 9 img.src = img.src + "?" + new Date().getTime(); 10 } 11 </script> 12 <style> 13 </style> 14 15 <body> 16 <form> 17 用户名: 18 <input type="text" name="username"> 19 <br /> 20 密码: 21 <input type="password" name="password"> 22 <br /> 23 认证码: 24 <input type="text" name="checkcode" /> 25 <img src="/ServletDemo/servlet/ResponseDemo" 26 onclick="changeImage(this)" alt="换一张" style="cursor:hand" /> 27 <br /> 28 <input type="submit" value="注册" /> 29 </form> 30 </body> 31 </html>
标签:style blog http java color width
原文地址:http://www.cnblogs.com/aineko/p/3821093.html