基于颜色序列的验证码java源.doc_第1页
基于颜色序列的验证码java源.doc_第2页
基于颜色序列的验证码java源.doc_第3页
基于颜色序列的验证码java源.doc_第4页
基于颜色序列的验证码java源.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package com.chb.yanzhengma;import java.awt.Color;import java.awt.EventQueue;import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.JButton;import java.awt.Font;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Random;import javax.swing.JEditorPane;import javax.swing.SwingConstants;import java.awt.GridLayout;import javax.swing.JLabel;public class CheckImage Color cl = new Color4;/颜色数组int a =0,1,2,3;int RD=0,0,0,0;String yuanshi=;Random r = new Random();private JFrame frame;JPanel panel = new JPanel();private final JPanel panel_1 = new JPanel();private final JPanel panel_3 = new JPanel();private final JEditorPane editorPane = new JEditorPane();private final JTextField textField = new JTextField();private final JButton btnHuan = new JButton(提交);private final JLabel lblNewLabel = new JLabel(验证码为: );private final JLabel label = new JLabel(验证码区域);private final JLabel label_1 = new JLabel(输入验证码);public static void main(String args) CheckImage window = new CheckImage();/window.createImag();window.frame.setVisible(true);public CheckImage() initialize();private void initialize() /窗口布局frame = new JFrame();frame.setAlwaysOnTop(true);frame.setBounds(100, 100, 450, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 0);frame.getContentPane().add(panel_1);panel_1.add(label);frame.getContentPane().add(panel);panel.add(label_1);frame.getContentPane().add(panel_3);panel_3.add(lblNewLabel);label.setForeground(Color.RED);/验证码区域设置editorPane.setFont(new Font(宋体, Font.PLAIN, 64);editorPane.setBounds(0, 0, 200, 80);panel_1.add(editorPane);label_1.setForeground(Color.GREEN);/输入区域的设置textField.setFont(new Font(宋体, Font.PLAIN, 55);textField.setColumns(7);textField.setBounds(0, 0, 200, 80);panel.add(textField);lblNewLabel.setFont(new Font(宋体, Font.PLAIN, 25);/按键设置btnHuan.setHorizontalAlignment(SwingConstants.LEFT);btnHuan.setFont(new Font(宋体, Font.PLAIN, 25);panel_3.add(btnHuan);frame.pack();/createImag(editorPane);/点击验证码区域更换图片事件editorPane.addMouseListener(new MouseListener()public void mouseClicked(MouseEvent arg0) textField.setText(null);createImag();editorPane.getCaret().setVisible(false);public void mouseEntered(MouseEvent arg0) public void mouseExited(MouseEvent arg0) public void mousePressed(MouseEvent arg0) public void mouseReleased(MouseEvent arg0) );/点击提交按钮,提交验证码并且验证事件btnHuan.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) String s = textField.getText();if(s!=&s.equals(yuanshi)lblNewLabel.setText(验证码输入正确);elseSystem.out.print(wrong);createImag();textField.setText(null););/*产生n个在0到n无重复数字的随机序列并将这个序列存入一个数组。 * * */void createRandom(int len,int aa)while(len0)Random r1 = new Random();int temp=r1.nextInt(len);int tt = aatemp;aatemp=aalen;aalen=tt;/System.out.println(aalen);len-;/System.out.println(aalen);/产生验证码的方法public void createImag()int lenth=3;/创建随机数类int width = 200, /图片宽度height = 80, /图片高度fontSize = 20, /字体大小codeLength = 4; /验证码长度/获得验证码editorPane区域上下文Graphics g=editorPane.getGraphics();/设置图片的背景颜色并且填充整个区域g.setColor(new Color(255,255,255);g.fillRect(0,0, width, height);/设置字体 g.setFont(new Font(Times New Roman, 0, fontSize); g.setColor(createColor(r,170,200); /画100条干扰线,不让其他程序来识别该图片for(int i=0;i100;i+)int x = r.nextInt(width);int y = r.nextInt(height);int _x = r.nextInt(11);int _y = r.nextInt(11);g.drawLine(x, y, x+_x, y+_y);createRandom(lenth,a);/*我们将顺序产生的四种不同颜色的字符所对应的顺序颜色存入一个颜色数组cl里面; */StringBuffer ssrand = new StringBuffer();for(int i=0;icodeLength;i+)/String rand = String.valueOf(RDi);RDi=r.nextInt(10);String rand = String.valueOf(RDi);ssrand.append(rand);/追加到字符串中cli = new Color(20+r.nextInt(width),20+r.nextInt(width),20+r.nextInt(width),20+r.nextInt(width);g.setColor(cli); int x = (width/(codeLength+4) * i + r.nextInt(width/(codeLength*2); int y = r.nextInt(height-fontSize)+fontSize; g.drawString(rand,x,y); / System.out.print(rand);yuanshi = new String(ssrand);/* * 如产生 的随机序列是3、1、2、0.产生验证码颜色字符为:红8、黑7、绿3、黄4, * 画在验证码图片上的颜色矩形条序列为 黄、黑、绿、红 * 而我们要输入的验证码图片上的颜色序列所对应的字符序列,此列为4738.*/StringBuff

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论