计:用Swing中Graphics绘制图片,图片使用随机背景色,随机字符串;绘制字符串随机位置,随机大小,随机颜色;背景中加入若干随机位置和颜色的混乱线、混乱点。

效果:

验证码生成器
验证码生成器
源代码:

package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AuthCode extends JDialog implements ActionListener
{

 

 private JPanel imagePanel, buttonPanel;
 private JTextField field;
 private JButton okButton, changeButton;
 private Random random;
 private BufferedImage image;
 private String[] s;

 public AuthCode()
 {
  init();
  setTitle("验证码");
  Toolkit kit = Toolkit.getDefaultToolkit();
  Dimension s = kit.getScreenSize();
  int screenWidth = s.width;
  int screenHeight = s.height;
  setBounds(screenWidth / 3, screenHeight / 3, 300, 200);
  setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 }

 public void init()
 {
  imagePanel = new JPanel();
  field = new JTextField();
  buttonPanel = new JPanel();

  okButton = new JButton("确定");
  changeButton = new JButton("更换");

  setImage();
  imagePanel.add(new JLabel(new ImageIcon(image)));
  buttonPanel.add(changeButton);
  buttonPanel.add(okButton);

  add(imagePanel, BorderLayout.NORTH);
  add(field, BorderLayout.CENTER);
  add(buttonPanel, BorderLayout.SOUTH);

  okButton.addActionListener(this);
  changeButton.addActionListener(this);

 }

 public static void main(String[] args)
 {
  new AuthCode().setVisible(true);
 }

 
 public void setImage()
 {
  // 创建内存图像
  int width = 200, height = 90;
  image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics g = image.getGraphics();
  // 取得随机数对象
  random = new Random();
  // 填充矩形区域
  g.setColor(new Color(random.nextInt(255), random.nextInt(255), random
    .nextInt(255)));
  g.fillRect(0, 0, width, height);
  // 画矩形边框
  g.setColor(new Color(255, 255, 255));
  g.drawRect(0, 0, width - 1, height - 1);
  // 画1000个混乱点
  for (int i = 0; i < 1000; i++)
  {

   int x = random.nextInt(width);
   int y = random.nextInt(height);
   int xl = x;
   int yl = y;
   g.drawLine(x, y, xl, yl);
   g.setColor(new Color(20 + random.nextInt(200), 20 + random
     .nextInt(200), 20 + random.nextInt(200)));
  }
  // 画100条混乱线
  for (int i = 0; i < 100; i++)
  {

   int x = random.nextInt(width);
   int y = random.nextInt(height);
   int xl = random.nextInt(100);
   int yl = random.nextInt(100);
   g.drawLine(x, y, x + xl, y + yl);
   g.setColor(new Color(20 + random.nextInt(200), 20 + random
     .nextInt(200), 20 + random.nextInt(200)));
  }
  // 随机获取四个字母或数字型字符
  s = new String[4];
  s[0] = setRndomString()[random.nextInt(62)];
  s[1] = setRndomString()[random.nextInt(62)];
  s[2] = setRndomString()[random.nextInt(62)];
  s[3] = setRndomString()[random.nextInt(62)];
  // 随机生成四个字符大小和位置,并将字符画到图片上
  g.setFont(new Font("Times New Roman", Font.PLAIN, 30 + random
    .nextInt(20)));
  g.setColor(new Color(20 + random.nextInt(110),
    20 + random.nextInt(110), 20 + random.nextInt(110)));
  g.drawString(s[0], 20 + random.nextInt(15), 40 + random.nextInt(30));
  g.setFont(new Font("Times New Roman", Font.PLAIN, 30 + random
    .nextInt(20)));
  g.setColor(new Color(20 + random.nextInt(110),
    20 + random.nextInt(110), 20 + random.nextInt(110)));
  g.drawString(s[1], 50 + random.nextInt(15), 40 + random.nextInt(30));
  g.setFont(new Font("Times New Roman", Font.PLAIN, 30 + random
    .nextInt(20)));
  g.setColor(new Color(20 + random.nextInt(110),
    20 + random.nextInt(110), 20 + random.nextInt(110)));
  g.drawString(s[2], 90 + random.nextInt(15), 40 + random.nextInt(30));
  g.setFont(new Font("Times New Roman", Font.PLAIN, 30 + random
    .nextInt(20)));
  g.setColor(new Color(20 + random.nextInt(110),
    20 + random.nextInt(110), 20 + random.nextInt(110)));
  g.drawString(s[3], 130 + random.nextInt(15), 40 + random.nextInt(30));
  g.dispose();
 }

 
 public String[] setRndomString()
 {
  String[] s = new String[62];
  for (int i = 0; i < 10; i++)
  {
   s[i] = Integer.toString(i);
  }
  for (int i = 10; i < 36; i++)
  {
   s[i] = Character.toString((char) ('a' + i - 10));
  }
  for (int i = 36; i < 62; i++)
  {
   s[i] = Character.toString((char) ('A' + i - 36));
  }
  return s;
 }

 public void actionPerformed(ActionEvent e)
 {
  if (e.getSource() == okButton)
  {
   if (field.getText().toLowerCase().equals(
     s[0].toLowerCase() + s[1].toLowerCase()
       + s[2].toLowerCase() + s[3].toLowerCase()))
   {
    JOptionPane.showMessageDialog(this, "输入正确,登陆成功!");
   } else
   {
    JOptionPane.showMessageDialog(this, "输入错误,登陆失败!");
   }
  } else
  {
   setImage();
   imagePanel.removeAll();
   imagePanel.add(new JLabel(new ImageIcon(image)));
   imagePanel.updateUI();
  }
 }

}

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐