关闭

验证码

704人阅读 评论(0) 收藏 举报

*
* Created on 2005-8-12
*
*
*/
package hp.alex.code;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
* @author Wan,bin
*

*/
public class RandomCode extends Panel {

  private static Random random = null;
  //content of the Code
  private String code = null;
  //height of the code
  public final static int CODE_HEIGHT = 20;
  //code width split
  public final static int CODE_WIDTH_SPLIT = 15;
  //code width single
  public final static int SINGLE_CODE_WIDTH = 6;
  //random line number
  public final static int RANDOM_LINE_NUM = 155;
  //code font
  public final static Font font = new Font("Arial", Font.PLAIN, 18);
  //code number
  private int codeNum = 0;
  //all length
  private int width = 0;
  //height
  private int height = 0;

  static {
    //initialize the random
    random = new Random();
  }

  /**
   *
   * @param start
   *            start of the color
   * @param finish
   *            end of the color
   * @return a new Color with the random
   *
   * return a new random color over the given range
   */
  private Color getColorOverRange(int start, int finish) {
    final int range = finish - start;
    if (range >= 0) {
      if (start > 255)
        start = 255;
      if (finish > 255)
        finish = 255;
      int r = start + random.nextInt(range);
      int g = start + random.nextInt(range);
      int b = start + random.nextInt(range);
      return new Color(r, g, b);
    } else {
      return Color.BLACK;
    }
  }

  /**
   *
   * @param length
   * @return image
   *
   * return the image with the code
   */

  private BufferedImage drawImage(int length) {
    width = (length + 1) * CODE_WIDTH_SPLIT;
    height = CODE_HEIGHT;
    BufferedImage image = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_RGB);

    Graphics g = image.getGraphics();

    //set the background color
    g.setColor(getColorOverRange(200, 250));
    g.fillRect(0, 0, width, height);

    //set font of the code
    g.setFont(font);

    //draw the border
    g.setColor(new Color(255, 255, 255));
    g.drawRect(0, 0, width - 1, height - 1);

    //random generate some line in order not to get the code by other
    // program easily.
    g.setColor(getColorOverRange(160, 200));
    for (int i = 0; i < RANDOM_LINE_NUM; 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);
    }

    //generate the random code
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < length; i++) {
      String rand = String.valueOf(random.nextInt(10));
      buffer.append(rand);
      //draw the string to the image
      g.setColor(new Color(20 + random.nextInt(110), 20 + random
          .nextInt(110), 20 + random.nextInt(110)));
      g.drawString(rand, CODE_WIDTH_SPLIT * i + SINGLE_CODE_WIDTH,
          CODE_HEIGHT - 4);
    }
    code = buffer.toString();
    g.dispose();
    return image;
  }

  /*
   * (non-Javadoc)
   *
   * @see java.awt.Component#paint(java.awt.Graphics)
   */
  public void paint(Graphics g) {
    if (codeNum <= 0)
      codeNum = 4;
    g.drawImage(drawImage(codeNum), 0, 0, width, height, null);
  }
  public RandomCode(int codeNum) {
    this.codeNum = codeNum;
  }
  /**
   * @return Returns the code.
   */
  public String getCode() {
    return code;
  }
}

 


 

/*
* Created on 2005-8-12
*
*/
package hp.alex.code;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Panel;

/**
* @author Wan,bin

*/
public class App extends Applet {
  /*
   * (non-Javadoc)
   *
   * @see java.applet.Applet#init()
   */
  public void init() {
    // TODO Auto-generated method stub
    Panel panel = new RandomCode(5);
    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);
    setVisible(true);
    setSize(100,20);
  }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24873次
    • 积分:345
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:14篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论