//在有些网站的注册和登录的时候会要用到验证码,是这样生成的
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import cn.com.csuinfosoft.window.CenterWindow;
/**
* 产生验证码
* @author 黄根华
*/
public class ValidateCodeImage extends JFrame {
public ValidateCodeImage() {
this.setSize(100, 120);
final ValidateCodeLabel codeLabel = new ValidateCodeLabel();
final JLabel codeTitle = new JLabel(codeLabel.getCode());
JButton codeButton = new JButton("改变下");
codeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
codeTitle.setText(codeLabel.changeCode());
}
});
// 用户点击图片也可以改变形状
codeLabel.setMouseListener(new ValidateCodeListener() {
public void codeChange(String code) {
codeTitle.setText(codeLabel.changeCode());
}
});
this.add(codeTitle, BorderLayout.NORTH);
this.add(codeLabel, BorderLayout.CENTER);
this.add(codeButton, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CenterWindow.centerWindow(this);
}
public static void main(String[] args) {
new ValidateCodeImage().setVisible(true);
}
class ValidateCodeLabel extends JLabel {
/** 可能产生的字符 中文会有乱码 */
private final static String CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
/** 要产生验证码长度 */
private final static int RANDCODELENGTH = 5;
/** 每个字符宽度 px */
private final static int EACHCODELENGTH = 15;
/** 左边空隙 系数 */
private final static double LEFT = 0.2;
/** 顶部空隙 系数 */
private final static double HEAD = 0.7;
/** 总共产生多少的噪声点,线 */
private final static int NOISECOUNT = 20;
/** 混乱系数 */
private final static double RANDOMFACTOR = 0.3;
private Color backGround, noise, line;
private String randCode;
private Random random = new Random();
public ValidateCodeLabel() {
this.backGround = new Color(240, 243, 248);
generateCode();
}
public ValidateCodeLabel(Color backGround, Color noise, Color line) {
this.backGround = backGround;
this.noise = noise;
this.line = line;
generateCode();
}
public void setMouseListener(final ValidateCodeListener listener) {
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
generateCode();
listener.codeChange(randCode);
}
});
}
protected void paintComponent(Graphics g) {
createValidateImage(g, randCode);
}
// Graphics 用于绘画 相当于MFC里面的CDC
private void createValidateImage(Graphics g, String randCode) {
Graphics2D graphic = (Graphics2D) g;
// 设定背景色
graphic.setColor(backGround);
int width = this.getWidth();
int height = this.getHeight();
graphic.fillRect(0, 0, width, height);
// 设定字体
graphic.setFont(new Font("Times New Roman", Font.PLAIN, 18));
for (int index = 0; index < randCode.length(); index++) {
// 将认证码显示到图象中
graphic.setColor(new Color(random.nextInt(255), random.nextInt(123),
random.nextInt(123)));
double radian = generateRadian();
graphic.rotate(radian, width / 2, height / 2);
graphic.drawString(String.valueOf(randCode.charAt(index)),
((int) (EACHCODELENGTH * index + width * LEFT)),
(int) (height * HEAD));
graphic.rotate(-radian, width / 2, height / 2);
}
// 干扰线
if (line != null) {
g.setColor(line);
for (int count = 0; count < NOISECOUNT; count++) {
int x = random.nextInt(width - 12);
int y = random.nextInt(height - 12);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
};
// 噪声点
if (noise != null) {
graphic.setColor(noise);
for (int count = 0; count < NOISECOUNT; count++) {
g.fillArc(random.nextInt(width - 3), random.nextInt(height - 3), 3,
3, 0, 360);
}
};
// 图象生效
graphic.dispose();
}
// 随机数
private double generateRadian() {
return random.nextDouble() * RANDOMFACTOR;
}
// 验证码产生
private void generateCode() {
StringBuffer code = new StringBuffer();
for (int index = 0; index < RANDCODELENGTH; index++) {
int randomNuber = random.nextInt(CODES.length());
code.append(CODES.substring(randomNuber, randomNuber + 1));
}
this.randCode = code.toString();
this.repaint();
}
/**
* 改变生成的验证码
*
* @return
*/
public String changeCode() {
generateCode();
return this.randCode.toLowerCase();
}
/**
* 得到当前生成的验证码
*
* @return
*/
public String getCode() {
return this.randCode.toLowerCase();
}
}
/**
* 用户点击了图片后的回调方法 作用类似于actionListener
*
* @author lonelybell
*
*/
class ValidateCodeListener {
public void codeChange(String code) {}
}
}