//2.3 画干扰线 g.setColor(Color.GREEN);// 设置干扰线颜色 for (int i = 1; i <= 7; i++) { //随机生成坐标点 //x1、y1即线的起始坐标,x2、y2即线的终点坐标 int x1 = ran.nextInt(width); int y1 = ran.nextInt(height); int x2 = ran.nextInt(width); int y2 = ran.nextInt(height); g.drawLine(x1, y1, x2, y2);//"画进内存" }
验证码实现
验证码的实现,原理就是字符串中随机取出四个值,打印到图片上
1 2 3 4 5 6 7 8 9 10
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";// 验证码就在这其中取 //生成随即图标 Random ran = new Random();
//写验证码 for (int i = 1; i <= 4; i++) { int index = ran.nextInt(str.length());//随机生成index下标 char ch = str.charAt(index);//取值 g.drawString(ch+"", width/10*(i*2), height/2);//打印 }
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //生成随即图标 Random ran = new Random();
//干扰线 g.setColor(Color.GREEN); for (int i = 1; i <= 7; i++) { //随机生成坐标点 int x1 = ran.nextInt(width); int y1 = ran.nextInt(height); int x2 = ran.nextInt(width); int y2 = ran.nextInt(height); g.drawLine(x1, y1, x2, y2); }
//验证码 for (int i = 1; i <= 4; i++) { int index = ran.nextInt(str.length()); char ch = str.charAt(index); g.drawString(ch+"", width/10*(i*2), height/2); }