您的位置:首页 > 移动开发 > 微信开发

用java写简单的验证码生成小程序 新手学java

2015-04-17 19:34 399 查看
用java写简单的验证码生成小程序 新手学java















import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Main {
public static void main(String[] args) {
final Frame frame = new Frame("验证码"); // 创建Frame对象
final Panel panel = new MyPanel();       // 创建Canvas对象
frame.add(panel);
frame.setSize(200, 100);
// 将Frame窗口居中
frame.setLocationRelativeTo(null);
frame.setVisible(true);

//窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
Window w=(Window)e.getComponent();
w.dispose();
}
});

}
}
class MyPanel extends Panel {
public void paint(Graphics g) {
int width = 160; // 定义验证码图片的宽度
int height = 40; // 定义验证码图片的高度
g.setColor(Color.LIGHT_GRAY);                // 设置上下文颜色
g.fillRect(0, 0, width, height);            // 填充验证码背景
g.setColor(Color.BLACK);                     // 设置上下文颜色
g.drawRect(0, 0, width - 1, height - 1); // 绘制边框
// 绘制干扰点
Random r = new Random();
for (int i = 0; i < 100; i++) {
int x = r.nextInt(width) - 2;
int y = r.nextInt(height) - 2;
g.drawOval(x, y, 2, 2);
}
g.setFont(new Font("黑体", Font.BOLD, 30)); // 设置验证码字体
g.setColor(Color.BLUE);                        // 设置验证码颜色
// 产生随机验证码
char[] chars = ("0123456789abcdefghijkmnopqrstuvwxyzABCDEFG"
+ "HIJKLMNPQRSTUVWXYZ").toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int pos = r.nextInt(chars.length);
char c = chars[pos];
sb.append(c + " ");
}
g.drawString(sb.toString(), 20, 30); // 写入验证码
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: