package org.cloudbus.cloudsim.codes.myproject.imagecode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

/**
 * ClassName ImageCode
 * Description
 * Create by HW
 * Date 2022/4/9 16:30
 */
public class ImageCode {
   static String[] strings={"a","b","c","d","e","f","g","h","i","j","k","m",
            "n","p","q","r","s","t","u","v","w","x","y","z","2","3","4","5","6","7","8","9","0"};

    public static void main(String[] args) throws Exception{
        /**
         * 1.画板、纸
         * 2、准备画笔
         * 3、准备一些数据，随机从这数据中抽取四个
         * 4、生成一张真正的图片
         * 5、将生成的图片进行存储
         */
        //定义图片的宽和高
        int width=600;
        int height=300;

        //1、画板中，JDK提供的画板类
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        //先获取画笔对象
        Graphics pen = bufferedImage.getGraphics();
        //设置画笔颜色
        pen.setColor(Color.black);

        pen.setFont(new Font("楷体",Font.BOLD,100));
        //设置画笔画的区域
        pen.fillRect(0,0,width,height);

        //继续设置画笔的颜色
        pen.setColor(Color.WHITE);

        int x=100,y=150;
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int num=random.nextInt(strings.length);
            String str=strings[num];
            pen.drawString(str,x,y);
            x+=100;
        }
        //画一点干扰线
        pen.drawLine(30,40,420,180);
        //把image的图片进行存储
        ImageIO.write(bufferedImage,"jpg",new File("E:\\研究生\\imageCode.jpg"));
    }
}
