java循环自动生成简单图片

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

public class Create {
        public static void main(String[] args) throws Exception{
            int i=0;
       //  while (true){
             int width = 1000;
             int height = 1000;
             String s=create();
             File file = new File("D:/testImage/image"+i+++".jpg");
             Font font = new Font("宋体", Font.PLAIN, 150);
             BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
             Graphics2D g2 = (Graphics2D)bi.getGraphics();
             g2.setBackground(Color.black);
             g2.clearRect(0, 0, width, height);
             g2.setPaint(Color.RED);
             g2.setFont(font);
             FontRenderContext context = g2.getFontRenderContext();
             Rectangle2D bounds = font.getStringBounds(s, context);
             double x = (width - bounds.getWidth()) / 2;
             double y = (height - bounds.getHeight()) / 2;
             double ascent = -bounds.getY();
             double baseY = y + ascent;
             g2.drawString(s, (int)x, (int)baseY);
             ImageIO.write(bi, "jpg", file);
             Thread.sleep(1000);
      //    }
       }
      static String  create(){
        String  s="";
        Random r = new Random();
        for(int i=0 ; i<5 ;  i++)
        {
            int ran1 = r.nextInt(1000);
            s+=ran1;
        }
        return s;
    }

    }
posted @ 2021-11-15 21:41  bobochen  阅读(185)  评论(0)    收藏  举报