Java爱心表白代码

 效果图

源码:
package cn.highedu.yxnu;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;

class Cardioid extends JFrame {

    private static final int WIDTH = 800;
    private static final int HEIGHT = 700;
    
    private static final int WINDOW_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    private static final int WINDOW_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    
    public Cardioid() {
        
        super("Love");
        this.setBackground(Color.BLACK);
        this.setLocation(500, 200);
        this.setSize(WIDTH, HEIGHT);
        this.setLayout(getLayout());
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.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(59, 193, (int) (20 * Math.random()) + 220);
                for (int i = 1; i < 400; 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 * 1 / 3;
                    z += (Math.PI / 2.0) / 80;
                    pic.setColor(color);
                    pic.fillOval((int) x, (int) y, 2, 2);
                }
                if (ii < 3) {
                    pic.setFont(new Font("楷体", Font.BOLD, 40));//设置字体
                    pic.setColor(Color.pink);
                    pic.drawString("Love You", WIDTH / 2 - 100, 240);//绘制字符串
                    pic.drawString("Forever", 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 Love {

    public static void main(String[] args) {

        new Cardioid();

    }

}


你可能感兴趣的:(java)