importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.FontMetrics;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.util.Random;importjavax.swing.JComponent;public class Yanzhencode extends JComponent implementsMouseListener {private String codes; //自动生成的验证码
private int width, height = 40; //设置验证码高度、宽度
private int codesLength = 4; //设置代码长度
private Random random = new Random(); //生成数字的方法
publicYanzhencode() {
width= this.codesLength * 16 + (this.codesLength - 1) * 10; //根据验证码长度设置宽度
setPreferredSize(new Dimension(width, height)); //设置背景大小
setSize(width, height); //设置验证码长度和宽度
this.addMouseListener(this);
setToolTipText("点击可更换验证码");
}//得到生成的验证码
public intgetCodesLength() {returncodesLength;
}//设置验证码的长度
public void setCodesLength(intcodeLength) {if(codesLength < 4) {this.codesLength = 4;
}else{this.codesLength =codeLength;
}
}publicString getCode() {returncodes;
}//让验证码产生随机的颜色
public Color getRandColor(int min, intmax) {if (min > 255)
min= 255;if (max > 255)
max= 255;int red = random.nextInt(max - min) +min;int green = random.nextInt(max - min) +min;int blue = random.nextInt(max - min) +min;return newColor(red, green, blue);
}//设置验证码具体的数字或字母是什么
protectedString generateCode() {char[] codes = new char[this.codesLength];for (int i = 0, len = codes.length; i < len; i++) {if(random.nextBoolean()) {
codes[i]= (char) (random.nextInt(10) + 48);
}else{
codes[i]= (char) (random.nextInt(26) + 97);
}
}this.codes = newString(codes);return this.codes;
}
@Overrideprotected voidpaintComponent(Graphics g) {super.paintComponent(g);if(this.codes == null || this.codes.length() != this.codesLength) { //判断生成的验证码是否为空或超出长度
this.codes =generateCode();
}
width= this.codesLength * 16 + (this.codesLength - 1) * 10;super.setSize(width, height); //接口使用,验证码字体大小
super.setPreferredSize(new Dimension(width, height));//接口使用,验证码背景大小
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //设置字体和字体大小
g.setFont(mFont); //设置对象//绘制出验证码的背景的矩形轮廓
Graphics2D g2d =(Graphics2D) g;
g2d.setColor(getRandColor(200, 250));
g2d.fillRect(0, 0, width, height);
g2d.setColor(getRandColor(180, 200));
g2d.drawRect(0, 0, width - 1, height - 1);//绘制出验证码背景的线
int i = 0, len = 150;for (; i < len; i++) {int x = random.nextInt(width - 1);int y = random.nextInt(height - 1);int x1 = random.nextInt(width - 10) + 10;int y1 = random.nextInt(height - 4) + 4;
g2d.setColor(getRandColor(180, 200));
g2d.drawLine(x, y, x1, y1);
}//绘制出验证码的具体字母
i = 0; len = this.codesLength;
FontMetrics fm=g2d.getFontMetrics();int base = (height - fm.getHeight())/2 +fm.getAscent();for(;i
g2d.rotate(random.nextInt(10)*0.01*b);
g2d.setColor(getRandColor(20, 130));
g2d.drawString(codes.charAt(i)+"", 16 * i + 10, base);
}
}//下一个验证码
public voidnextCode() {
generateCode();
repaint();
}
@Overridepublic voidmouseClicked(MouseEvent e) {
nextCode();
}
@Overridepublic voidmousePressed(MouseEvent e) {//TODO Auto-generated method stub
}
@Overridepublic voidmouseReleased(MouseEvent e) {//TODO Auto-generated method stub
}
@Overridepublic voidmouseEntered(MouseEvent e) {//TODO Auto-generated method stub
}
@Overridepublic voidmouseExited(MouseEvent e) {//TODO Auto-generated method stub
}
}