01 | package Demo; |
02 |
03 | import java.awt.Color; |
04 | import java.awt.Graphics; |
05 | import java.awt.Image; |
06 | import java.awt.Toolkit; |
07 |
08 | import javax.swing.JFrame; |
09 |
10 | @SuppressWarnings ( "serial" ) |
11 | class Cardioid extends JFrame |
12 | { |
13 | //定义窗口大小 |
14 | private static final int WIDTH = 480 ; |
15 | private static final int HEIGHT = 600 ; |
16 | |
17 | //获取屏幕大小 |
18 | private static int WINDOW_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; |
19 | private static int WINDOW_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; |
20 | |
21 | //构造函数 |
22 | public Cardioid(){ |
23 | //设置窗口标题 |
24 | super ( "❤形线" ); |
25 | //设置背景色 |
26 | this .setBackground(Color.BLACK); |
27 | //设置窗口位置 |
28 | this .setLocation((WINDOW_WIDTH - WIDTH)/ 2 , (WINDOW_HEIGHT - HEIGHT)/ 2 ); |
29 | //设置窗口大小 |
30 | this .setSize(WIDTH, HEIGHT); |
31 | //设置窗口布局 |
32 | this .setLayout(getLayout()); |
33 | //设置窗口可见 |
34 | this .setVisible( true ); |
35 | //设置窗口默认关闭方式 |
36 | this .setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
37 | } |
38 | public void paint(Graphics g){ |
39 | |
40 | double x,y,r; //横纵坐标及半径 |
41 | Image image = this .createImage(WIDTH, HEIGHT); |
42 | Graphics pic = image.getGraphics(); |
43 | |
44 | //绘制图形 |
45 | for ( int i = 0 ; i < 100 ; i++) |
46 | { |
47 | for ( int j = 0 ; j < 100 ; j++) |
48 | { |
49 | r = Math.PI / 45 + Math.PI / 45 * i * ( 1 - Math.sin(Math.PI / 45 * j)) * 18 ; |
50 | |
51 | x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + WIDTH / 2 ; |
52 | |
53 | y = -r*Math.sin(Math.PI / 45 * j) + HEIGHT / 2 ; |
54 | |
55 | pic.setColor(Color.MAGENTA); |
56 | pic.fillOval(( int )x, ( int )y, 2 , 2 ); |
57 | } |
58 | //生成图片 |
59 | g.drawImage(image, 0 , 0 , this ); |
60 | |
61 | } |
62 | |
63 | } |
64 | } |
65 | public class CardioidDemo{ |
66 | |
67 | public static void main(String[] args){ |
68 | |
69 | new Cardioid(); |
70 | } |
71 | } |