java 制作爱心

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

class Cardioid extends JFrame {

//定义窗口大小

    private static final int WIDTH = 900;

    private static final int HEIGHT = 800;

//获取屏幕大小

    private static final int WINDOW_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;

    private static final int WINDOW_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;

//构造函数

    public Cardioid() {

//设置窗口标题

        super("♥爱心");

//设置背景色

        this.setBackground(Color.BLACK);

//设置窗口位置

        this.setLocation((WINDOW_WIDTH - WIDTH) / 2, (WINDOW_HEIGHT - HEIGHT) / 2);

//设置窗口大小

        this.setSize(WIDTH, HEIGHT);

//设置窗口布局

        this.setLayout(getLayout());

//设置窗口可见

        this.setVisible(true);

//设置窗口的默认关闭方式

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

//图形函数

    public void paint(Graphics g) {

//横纵坐标及半径

        double x, y, r;



//绘制图形
        double z = 0.0;
        double size=10;
        int jj=0;
        while (true) {

            Image image = this.createImage(WIDTH, HEIGHT);

            Graphics pic = image.getGraphics();
            if (jj%2==0){
                size=14.5;
            }else {
                size=15;
            }
            for (int ii = 30; ii > 0; ii--) {
                Color color = new Color(106, 191, (int) (20 * Math.random()) + 220);
                for (int i = 1; i < 400; i++) {
//                try {
//                    Thread.sleep(10);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                    int px = (int) (Math.random() * 10);
                    int py = (int) (Math.random() * 10);
                    x = 16 * (Math.sin(z) * Math.sin(z) * Math.sin(z)) * (size) + Math.pow((-1), px) * Math.random() * ii * Math.sqrt(ii) + WIDTH / 2;
                    y = -(13 * Math.cos(z) - 5 * Math.cos(2 * z) - 2 * Math.cos(3 * z) - Math.cos(4 * z)) * (size) + Math.pow((-1), py) * Math.random() * ii * Math.sqrt(ii) + HEIGHT * 1 / 3;
                    z += (Math.PI / 2.0) / 80;
                    pic.setColor(color);
//                pic.setColor(new Color(0, 255, 255));
                    pic.fillOval((int) x, (int) y, 2, 2);


//                g.drawImage(image, 0, 0, this);

                }
//                if (ii < 12) {
//                    g.drawImage(image, 0, 0, this);
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
                if (ii < 3) {
                    pic.setFont(new Font("楷体", Font.BOLD, 40));//设置字体
                    pic.setColor(Color.pink);
                    pic.drawString("Love You", WIDTH / 2 - 100, 240);//绘制字符串
                    pic.drawString("佳庆", WIDTH / 2, 280);//绘制字符串
                    g.drawImage(image, 0, 0, this);
                }
            }
            jj++;
            if (jj>100){
                break;
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

public class 爱心 {

    public static void main(String[] args) {

        new Cardioid();

    }

}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值