心形曲线

 1 package Test;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.Image;
 6 import java.awt.Toolkit;
 7 
 8 import javax.swing.JFrame;
 9 
10 public class Love extends JFrame {
11     
12     private static final int width = 480;
13     private static final int height = 600;
14     
15     private static int window_width = Toolkit.getDefaultToolkit().getScreenSize().width;
16     private static int window_height = Toolkit.getDefaultToolkit().getScreenSize().height;
17 
18     public Love()
19     {
20         super("心形线");
21         this.setBackground(Color.BLACK);
22         this.setLocation((window_width - width) / 2, (window_height - height) / 2);
23         this.setSize(width, height);
24         this.setLayout(getLayout());
25         this.setVisible(true);
26         this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
27     }
28     
29     public void paint(Graphics g)
30     {
31         double x, y, r;
32         Image image = this.createImage(width, height);
33         Graphics pic = image.getGraphics();
34         
35         for(int i = 0; i < 100; i++)
36         {
37             for(int j = 0; j < 100; j++)
38             {
39                 r = Math.PI / 45 + Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * 18;
40                 x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + width / 2;
41                 y = -r * Math.sin(Math.PI / 45 * j) + height / 2;
42                 pic.setColor(Color.MAGENTA);
43                 pic.fillOval((int)x, (int)y, 2, 2);
44             }
45             g.drawImage(image, 0, 0, this);
46         }
47     }
48     
49     public static void main(String[] args) {
50         new Love();
51     }
52 }

原文地址:https://www.cnblogs.com/fylove/p/6213912.html