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 閱讀(177) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 揭东县| 开封市| 锡林浩特市| 连城县| 醴陵市| 肃宁县| 苏尼特左旗| 湄潭县| 安龙县| 巴马| 天祝| 随州市| 光山县| 庆安县| 汨罗市| 松溪县| 沁源县| 沁水县| 岚皋县| 阳江市| 三明市| 城步| 会泽县| 淅川县| 萝北县| 洛南县| 临安市| 阿拉善右旗| 琼中| 徐州市| 伊吾县| 大安市| 岑溪市| 阳泉市| 南雄市| 揭东县| 加查县| 司法| 高平市| 青冈县| 宁德市|