星期四

改了一下验证码的代码,加了输入错误刷新图片,文字大小,颜色随机,随机小幅度旋转
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.awt.image.renderable.RenderableImage;
import java.text.AttributedCharacterIterator;
import java.util.Map;
import java.util.Random;

public class VerificationCode extends JFrame {
    private String code = "";
    private JTextField codeText;
    private JButton button1;

    VerificationCode() {
        super();
        setResizable(false);
        setTitle("Java实现验证码功能");
        setBounds(750, 400, 600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        getContentPane().add(panel, BorderLayout.CENTER);
        JLabel label1 = new JLabel();
        label1.setText("验证码:");
        label1.setBounds(10, 100, 50, 20);
        panel.add(label1);
        JTextField codeText = new JTextField();
        codeText.setBounds(60, 100, 100, 20);
        panel.add(codeText);
        JButton button1 = new JButton("确定");
        button1.setBounds(60, 140, 80, 20);
        panel.add(button1);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String info = "";
                if (!codeText.getText().equalsIgnoreCase(code)) {
                    info = "验证码错误";
                    code="";
                    draw();
                    }
                else
                    System.exit(0);
                JOptionPane.showMessageDialog(null, info);
            }
        });
    }

    public void paint(Graphics g){
        BufferedImage image = new BufferedImage(200, 100, 1);
        Graphics2D graphics = image.createGraphics();
        AffineTransform transform=new AffineTransform();
        Stroke stroke = new BasicStroke(2.0f);
        Random r = new Random();
        for (int i = 1; i <= 8; i++) {
            char c;
            double a;
            String C="";
            switch (r.nextInt(2) + 1) {
                case 1:
                    c = (char)(r.nextInt(9) + 48);
                    C+=c;
                    a=(r.nextInt(90)+315)*3.14/180;
                    graphics.setStroke(stroke);
                    graphics.setColor(new Color(r.nextInt()));
                    graphics.setFont(new Font("仿宋",Font.BOLD,r.nextInt(10)+20));
                    transform.rotate(a,i*20,50);
                    graphics.setTransform(transform);
                    graphics.drawString(C, i * 20, 50);
                    transform.rotate(-a,i*20,50);
                    graphics.setTransform(transform);
                    code += c;
                    break;
                case 2:
                    a=(r.nextInt(90)+315)*3.14/180;
                    c = (char) (r.nextInt(26) + 65);
                    graphics.setStroke(stroke);
                    graphics.setColor(new Color(r.nextInt()));
                    graphics.setFont(new Font("仿宋",Font.BOLD,r.nextInt(10)+20));
                    transform.rotate(a,i*20,50);
                    graphics.setTransform(transform);
                    graphics.drawString(C+c, i * 20, 50);
                    graphics.setTransform(new AffineTransform());
                    transform.rotate(-a,i*20,50);
                    graphics.setTransform(transform);
                    code += c;
                    break;
            }
        }
        g.drawImage(image, 40, 20, null);
    }
    public void draw()
    {
        repaint();
    }
    public static void main(String[] args) {
        VerificationCode frame = new VerificationCode();
        frame.setVisible(true);
    }

 


 

posted @ 2023-09-15 16:56  umiQa  阅读(13)  评论(0编辑  收藏  举报