⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkcode.java

📁 一个由jsp+servlet+MySQL简单的新闻发布系统
💻 JAVA
字号:
package com.news.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Random;
/*
 * 生成验证码
 */
public class CheckCode {

	private String sRand = "";

	private BufferedImage image;

	// 设置宽度,高度
	private int width = 60, height = 20;

	// 设置数字个数
	private int numCount = 4;

	public CheckCode() {			//构造器后面逐次重载
	}

	public CheckCode(int numCount) {//
		this.numCount = numCount;
	}

	public CheckCode(int width, int height) {//
		this.width = width;
		this.height = height;
	}

	public CheckCode(int width, int height, int numCount) {//
		this.width = width;
		this.height = height;
		this.numCount = numCount;
	}

	public void generator() {
		image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

		g.setColor(getRandColor(160, 200));
		// 获取155条随机干扰线
		Random random = new Random();
		for (int i = 0; i < 155; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}
		// 产生四个随机数字
		for (int i = 0; i < numCount; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
			g.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));
			g.drawString(rand, width / numCount * i + 3, 16);
		}
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getNumCount() {
		return numCount;
	}

	public void setNumCount(int numCount) {
		this.numCount = numCount;
	}

	// 获取随机颜色值
	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	// 获取随机图片
	public Image getRandomImage() {
		return image;
	}

	public String getRand() {
		return sRand;
	}
}




	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -