使用java程序生成具有3D效果的图片

程序功能

图像初始化:创建一个800x800像素的 BufferedImage 对象,并使用 Graphics2D 进行绘图操作。
光照效果:使用 RadialGradientPaint 实现径向渐变效果,模拟从图片中心向外扩散的光照,使图片有深度感,营造出三维空间的效果。
随机几何图形:在图片上随机生成10个带有阴影的椭圆形。每个椭圆形的颜色、大小、位置都是随机的,椭圆图形具有立体感的黑色阴影。
边缘高光:为每个椭圆的边缘添加半透明的高光线条,模拟光照反射,增强3D视觉效果。
文件保存:生成的图片以PNG格式保存,文件名为当前时间戳加上随机数,确保文件名唯一。
在这里插入图片描述

代码

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;  // 导入 Point2D
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Random;

public class Artistic3DImageGenerator {

    public static void main(String[] args) {
        int width = 800;  // 图片宽度
        int height = 800; // 图片高度

        // 创建一个BufferedImage对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 使用Graphics2D绘图
        Graphics2D graphics = image.createGraphics();

        // 设置抗锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 随机生成颜色渐变光照效果
        Random random = new Random();
        Color color1 = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        Color color2 = new Color(255, 255, 255); // 白色光源

        // 使用径向渐变模拟光照效果
        Point2D center = new Point2D.Float(width / 2, height / 2);
        float radius = width / 2f;
        float[] dist = {0.0f, 1.0f};
        Color[] colors = {color2, color1};
        RadialGradientPaint radialGradient = new RadialGradientPaint(center, radius, dist, colors);
        graphics.setPaint(radialGradient);
        graphics.fillRect(0, 0, width, height);

        // 在图片上添加带有阴影效果的几何图形
        for (int i = 0; i < 10; i++) {
            // 随机选择几何图形的颜色
            Color shapeColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            graphics.setColor(shapeColor);

            // 随机生成形状的大小和位置
            int shapeWidth = random.nextInt(200) + 100;
            int shapeHeight = random.nextInt(200) + 100;
            int x = random.nextInt(width - shapeWidth);
            int y = random.nextInt(height - shapeHeight);

            // 绘制带有阴影的椭圆形
            graphics.setColor(Color.BLACK);
            graphics.fillOval(x + 10, y + 10, shapeWidth, shapeHeight); // 绘制阴影
            graphics.setColor(shapeColor);
            graphics.fillOval(x, y, shapeWidth, shapeHeight);

            // 为椭圆添加边缘高光,增强3D效果
            graphics.setColor(new Color(255, 255, 255, 128)); // 半透明高光
            graphics.setStroke(new BasicStroke(5));
            graphics.draw(new Ellipse2D.Float(x, y, shapeWidth, shapeHeight));
        }

        // 释放图形资源
        graphics.dispose();

        // 生成随机文件名
        String fileName = "artistic_3d_image_" + System.currentTimeMillis() + "_" + random.nextInt(10000) + ".png";

        // 将图片保存为PNG文件
        try {
            ImageIO.write(image, "png", new File(fileName));
            System.out.println("3D艺术图片已生成!文件名为: " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 12
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值