制作验证码

我们很经常会遇到需要输入验证码的情况,那么,验证码到底是怎么生成的呢?

其实这是利用java的画板做的,需要一些随机数,然后画到画板上面。

可以使用JPanel的paint,也可以使用Canvas的paint 来实现。

但通常都是使用JPanel来实现,因为JPanel是Swing包的而且效率比较高。

下面分别展现一下这两种实现方式:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class IDCode extends JFrame {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		new IDCode();
	}

	public IDCode() {
		super("验证码生成");
		setBounds(300, 100, 300, 150);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		MyJPanel myJPanel = new MyJPanel();
		panel.add(myJPanel,BorderLayout.CENTER);// 默认加在Center
		JButton button = new JButton("看不清");
		panel.add(button,BorderLayout.SOUTH);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				repaint();
			}
		});
		getContentPane().add(panel);
		setVisible(true);
	}
}

class MyJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private Random random = new Random(System.currentTimeMillis());

	@Override
	public void paint(Graphics g) {
		//所有的参数都需要根据实际情况来设定
                g.setColor(Color.white);
		g.fillRect(85, 20, 120, 50);
		
		//画数字
		g.setFont(new Font("黑体", Font.BOLD, 35));
		for (int i = 0; i < 5; i++) {
			g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
			g.drawString("" + random.nextInt(10), 95 + i * 19, 60 + random.nextInt(10));
		}
		
		//画干扰线
		int count = 0;
		while(count < 5){
			int x1 = random.nextInt(120)+85;
			int y1 = random.nextInt(50)+20;
			int x2 = random.nextInt(120)+85;
			int y2 = random.nextInt(50)+20;
			if(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))>80){
				g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
				g.drawLine(x1, y1, x2, y2);
				count++;
			}
		}
	}
}

下面是Canvas的实现(部分代码):

class MyCanvas extends Canvas {
	private static final long serialVersionUID = 1L;
	private Random r = new Random(System.currentTimeMillis());

	@Override
	public void paint(Graphics g) {
		// setBackground(Color.white);
		g.setColor(Color.white);
		g.fillRect(10, 10, 100, 50);

		g.setFont(new Font("aa", Font.BOLD, 20));
		for (int i = 0; i < 5; i++) {
			g.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
			int a = r.nextInt(10);
			int n = r.nextInt(10);
			g.drawString("" + a, 50 + i * 10, 45 + n);
		}

		int i = 0;
		while (i < 5) {
			g.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
			int x1 = r.nextInt(100);
			int y1 = r.nextInt(60);
			int x2 = r.nextInt(100);
			int y2 = r.nextInt(60);
			if (Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) < 50) {
				continue;
			}
			g.drawLine(x1, y1, x2, y2);
			i++;
		}

	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值