Java 实现画图并保存在桌面

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;

/**
 * @ 开发环境:IntelliJ IDEA
 * @ 项目名称:Test
 * @ 作者:ALIM- MASTIK
 * @ 邮箱:1269802307@qq.com
 * @ 创建时间:2023/10/23 18:44
 */
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class FiveStarFlag extends JPanel {
    private int width, height;
    private double maxR = 0.15, minR = 0.05;
    private double maxX = 0.25, maxY = 0.25;
    private double[] minX = {0.50, 0.60, 0.60, 0.50};
    private double[] minY = {0.10, 0.20, 0.35, 0.45};
    final BufferedImage bufferedImage=new BufferedImage(900,600,BufferedImage.TYPE_INT_RGB);
    File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();

    String desktopPath = desktopDir.getAbsolutePath();


    public static void main(String[] args) {
        JFrame jFrame = new JFrame("五星红旗");
        jFrame.getContentPane().add(new FiveStarFlag(1500));
        jFrame.pack();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        //jFrame.setVisible(true);
    }
    public static Shape createPentacle(double sx, double sy, double radius, double theta) {
        final double arc = Math.PI / 5;
        final double rad = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10);
        GeneralPath path = new GeneralPath();
        path.moveTo(1, 0);
        for (int i = 0; i < 5; i++) {
            path.lineTo(rad * Math.cos((1 + 2 * i) * arc), rad * Math.sin((1 + 2 * i) * arc));
            path.lineTo(Math.cos(2 * (i + 1) * arc), Math.sin(2 * (i + 1) * arc));
        }
        path.closePath();
        AffineTransform atf = AffineTransform.getScaleInstance(radius, radius);
        atf.translate(sx / radius, sy / radius);
        atf.rotate(theta);
        return atf.createTransformedShape(path);
    }



    /**
     * 创建一个宽度为width的国旗
     */
    public FiveStarFlag(int width) {
        this.width = width / 3 * 3;
        this.height = width / 3 * 2;
        setPreferredSize(new Dimension(this.width, this.height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D graphics;
        graphics=bufferedImage.createGraphics();

        Graphics2D graphics2D = (Graphics2D) g;
        //画旗面
        graphics2D.setPaint(Color.RED);
        graphics.setPaint(Color.RED);
        graphics2D.fillRect(0, 0, width, height);
        graphics.fillRect(0, 0, width, height);
        //画大☆
        double ox = height * maxX, oy = height * maxY;
        graphics2D.setPaint(Color.YELLOW);
        graphics.setPaint(Color.YELLOW);
        graphics2D.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2));
        graphics.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2));
        //画小★
        for (int i = 0; i < 4; i++) {
            double sx = minX[i] * height, sy = minY[i] * height;
            double theta = Math.atan2(oy - sy, ox - sx);
            graphics2D.fill(createPentacle(sx, sy, height * minR, theta));
            graphics.fill(createPentacle(sx, sy, height * minR, theta));
        }
        File f = new File(desktopPath+"\\五星红旗.jpg");
        try {
            ImageIO.write(bufferedImage, "jpg", f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALIM-MASTIK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值