JAVA--生成验证码

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;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CodeMatch extends JFrame{
   
    
    public CodeMatch(){
        
    }
    
    public static void main(String[] args){
        int width = 100;
        int height = 40;
        BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        g.setColor(new Color(100,100,100));
        g.fillRect(0,0,width,height);
        Random md = new Random();
        int randnum = md.nextInt(9000)+1000;
        String randstr = String.valueOf(randnum);
        g.setColor(Color.BLACK);
        g.setFont(new Font("",Font.PLAIN,20));
        g.drawString(randstr,30, 30);
        for(int i=0;i<100;i++){
            int x = md.nextInt(width);
            int y = md.nextInt(height);
            g.drawOval(x,y,1,1);
            
        }
        
        Image image = (Image)img;
       Panel pane = new Panel(image);
        
        JFrame frame = new JFrame();
        frame.add(pane);
        frame.setSize(100,70);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Panel extends JPanel{
    Image img;
    public Panel(Image img){
        this.img = img;
        repaint();
    }
    
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值