wangflood

          精心維護一個技術blog,為了工作,也是愛好。

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 19 Stories :: 8 Comments :: 0 Trackbacks
                  最近在做一個面試題,斷斷續續地忙了九天。終于趕在圣誕節前,這個美好的晚上完成。不僅把SSH架構復習一遍,displaytag也應用上了。
          完成這個項目,產生了幾個衍生品。其中驗證碼就是其中一個。

          一般大登錄和注冊頁面上存在驗證,以前覺得很神秘。可能是接觸得多,高度就高了,再看驗證碼,其實就是response一個contentType="image/jpeg"類型的html而已。
          下面是我和awt方式生成這張圖片的源碼:
          import java.awt.BorderLayout;
          import java.awt.Button;
          import java.awt.Color;
          import java.awt.Font;
          import java.awt.Frame;
          import java.awt.Graphics;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.awt.event.WindowAdapter;
          import java.awt.event.WindowEvent;
          import java.awt.image.BufferedImage;
          import java.util.Random;
          import java.util.concurrent.TimeUnit;

          public class Test extends Frame {

              
          public Test() {

                  Button b 
          = new Button("下一個");

                  setBounds(
          300300400400);
                  
          this.addWindowListener(new WindowAdapter() {
                      @Override
                      
          public void windowClosing(WindowEvent e) {
                          System.exit(
          0);
                      }

                  }
          );
                  b.addActionListener(
          new ActionListener() {

                      @Override
                      
          public void actionPerformed(ActionEvent e) {
                          repaint();

                      }

                  }
          );
                  repaint();
                  
          this.setLayout(new BorderLayout());
                  
          this.add(b, BorderLayout.SOUTH);
                  
          this.setVisible(true);
              }


              @Override
              
          public void paint(Graphics headG) {
                  
          int width = 400, height = 400;
                  BufferedImage image 
          = new BufferedImage(width, height,
                          BufferedImage.TYPE_INT_RGB);
                  Graphics g 
          = image.getGraphics();
                  Random random 
          = new Random();
                  g.setColor(getRandColor(
          200250));
                  g.fillRect(
          00, width, height);
                  g.setFont(
          new Font("Arial", Font.BOLD, 120));
                  g.setColor(getRandColor(
          160200));

                  
          for (int i = 0; i < 550; i++{
                      
          int x = random.nextInt(width);
                      
          int y = random.nextInt(height);
                      
          int xl = random.nextInt(12);
                      
          int yl = random.nextInt(12);
                      g.drawLine(x, y, x 
          + xl, y + yl);
                  }

                  String sRand 
          = "";
                  
          for (int i = 0; i < 4; i++{
                      String rand 
          = String.valueOf(random.nextInt(10));
                      sRand 
          += rand;
                      g.setColor(
          new Color(000));
                      g.drawString(rand, 
          110 * i + 3250);
                  }


                  headG.drawImage(image, 
          00this);
              }


              
          public static void main(String[] args) {

                  
          new Test();

              }


              
          static Color getRandColor(int fc, int bc) {
                  Random random 
          = new Random();
                  
          if (fc > 255)
                      fc 
          = 255;
                  
          if (bc > 255)
                      bc 
          = 255;
                  
          int r = fc + random.nextInt(bc - fc);
                  
          int g = fc + random.nextInt(bc - fc);
                  
          int b = fc + random.nextInt(bc - fc);
                  
          return new Color(r, g, b);
              }


          }


          運行情況(截圖):

          posted on 2010-12-25 23:10 wangflood 閱讀(172) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 富民县| 通榆县| 哈巴河县| 霍山县| 蓬莱市| 崇文区| 靖宇县| 满洲里市| 金坛市| 抚顺县| 铁岭县| 凉城县| 容城县| 博客| 徐闻县| 腾冲县| 肥东县| 额济纳旗| 镇巴县| 邛崃市| 阿荣旗| 金沙县| 福建省| 和硕县| 民县| 宜宾市| 平陆县| 建始县| 延庆县| 拜泉县| 洛川县| 黔南| 本溪市| 固阳县| 大石桥市| 左云县| 翼城县| 苍南县| 林口县| 天峻县| 皋兰县|