刚才代码没有贴全,我重新贴一下- public class ResponseDemo4 extends HttpServlet {
- public static final int WIDTH=120;
- public static final int HEIGHT=25;
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- BufferedImage image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
-
- Graphics g=image.getGraphics();
-
- setBackGround(g);
-
- setBorder(g);
-
- drawRandomLine(g);
-
-
- drawRandomNum(Graphics g);
-
- ImageIO.write(image, "jpg", response.getOutputStream());
- }
- private void drawRandomNum(Graphics g) {
- g.setColor(Color.RED);
- g.setFont(new Font("宋体",Font.BOLD,20));
- String base="\u7684\u4e00\u4e86\u6211";
- int x=5;
- for(int i=0;i<4;i++){
- int degree=new Random().nextInt();
- String ch=base.charAt(i)+"";
- g.rotate(degree*Math.PI/180,x,20);
- g.drawString(ch, x, 20);
- g.rotate(-degree*Math.PI/180,x,20);
- x+=30;
- }
-
- }
- private void drawRandomLine(Graphics g) {
- g.setColor(Color.GREEN);
-
- for(int i=0;i<5;i++){
- int x1=new Random().nextInt(WIDTH);
- int y1=new Random().nextInt(HEIGHT);
-
-
- int x2=new Random().nextInt(WIDTH);
- int y2=new Random().nextInt(HEIGHT);
- g.drawLine(x1, y1, x2, y2);
- }
-
- }
- private void setBorder(Graphics g) {
- g.setColor(Color.WHITE);
- g.fillRect(1, 1, WIDTH-2, HEIGHT-2);
-
- }
- private void setBackGround(Graphics g) {
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, WIDTH, HEIGHT);
-
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- }
- }
复制代码 |