0


Java-520用IDEA实现动态爱心效果

爱心 2024-05-10 10-23-20

实现的主要效果如上面视频,只需IDEA用Java语言即可实现,下面是全部代码:

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

class MyFrame extends JFrame {
    private static final int WIDTH = 1200;
    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 void launchFrame() {
        this.setTitle("爱心");
        this.setBackground(Color.BLACK);
        this.setLocation((WINDOW_WIDTH - WIDTH) / 2, (WINDOW_HEIGHT - HEIGHT) / 2);
//设置窗口大小
        this.setSize(WIDTH, HEIGHT);
//设置窗口可见
        this.setVisible(true);
//窗口关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g) {
//横纵坐标及半径
        double x, y;
//绘制图形
        double z = 0.0;
        double size;
        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--) {
                    for (int i = 1; i < 1000; i++) {
                        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 / 3;
                        z += (Math.PI / 2.0) / 80;
                        pic.fillOval((int) x, (int) y + 50, 2, 2);
                        pic.setColor(new Color(255, 0, 102, 244));
                    }
                    if (ii < 3) {
                        g.drawImage(image, 0, 0, this);
                    }
                }
                jj++;
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
}
public class LoveHeart {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
        myFrame.launchFrame();
    }
}

本文转载自: https://blog.csdn.net/dakunming/article/details/138654959
版权归原作者 愛意随風起 所有, 如有侵权,请联系我们删除。

“Java-520用IDEA实现动态爱心效果”的评论:

还没有评论