wangflood

          精心維護(hù)一個(gè)技術(shù)blog,為了工作,也是愛好。

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            14 Posts :: 19 Stories :: 8 Comments :: 0 Trackbacks
                  最近在做一個(gè)面試題,斷斷續(xù)續(xù)地忙了九天。終于趕在圣誕節(jié)前,這個(gè)美好的晚上完成。不僅把SSH架構(gòu)復(fù)習(xí)一遍,displaytag也應(yīng)用上了。
          完成這個(gè)項(xiàng)目,產(chǎn)生了幾個(gè)衍生品。其中驗(yàn)證碼就是其中一個(gè)。

          一般大登錄和注冊(cè)頁(yè)面上存在驗(yàn)證,以前覺得很神秘。可能是接觸得多,高度就高了,再看驗(yàn)證碼,其實(shí)就是response一個(gè)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("下一個(gè)");

                  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);
              }


          }


          運(yùn)行情況(截圖):

          posted on 2010-12-25 23:10 wangflood 閱讀(172) 評(píng)論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 乃东县| 九江市| 甘肃省| 营山县| 祁门县| 六安市| 剑河县| 聂拉木县| 杂多县| 辽阳县| 泽州县| 合阳县| 冕宁县| 广元市| 项城市| 沂水县| 汾阳市| 华亭县| 南城县| 滦平县| 清远市| 新巴尔虎右旗| 印江| 湘潭市| 清水县| 通辽市| 镇坪县| 大同县| 济宁市| 融水| 开原市| 长宁区| 得荣县| 桃江县| 宁津县| 临朐县| 甘南县| 买车| 溆浦县| 广德县| 宝山区|