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 @   bobochen  阅读(175)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示