/**
*
* 画 随机干扰线
*
* @param g
* 图形上下文(画笔)
*
* @param count
* 干扰线条数
*/
private void drawLine(Graphics g,int count){
Random random = new Random();
// 循环绘制每条干扰线
for (int j = 0; j < count; j++) {
// 设置线条随机颜色
g.setColor(getRandomColor(180, 200));
// 生成随机线条起点终点,坐标点
int x1 = random.nextInt(this.width);
int y1 = random.nextInt(this.height);
int x2 = random.nextInt(this.width);
int y2 = random.nextInt(this.height);
// 画线条
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 获取随机颜色
*
* @param i
* 颜色下限值
* @param j
* 颜色上限值
* @return 随机颜色对象
*/
private Color getRandomColor(int i, int j) {
if (i > j) {
int tmp = i;
i = j;
j = tmp;
}
if (j > 225) {
j = 225;
}
if (i < 0) {
i = 0;
}
int r = i + (int) (Math.random() * (j - i));
int g = i + (int) (Math.random() * (j - i));
int b = i + (int) (Math.random() * (j - i));
return new Color(r, g, b);
// values in the range (0 - 255). red green blue
}
public static void main(String[] args) {
JFrame frame = new JFrame("验证码");
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
Verification cation = new Verification();
JLabel lbl = new JLabel(new ImageIcon(cation.getImage()));
frame.add(lbl);
frame.setVisible(true);
}
}