Java Cardioid 心脏形曲线 (整理)
生活随笔
收集整理的這篇文章主要介紹了
Java Cardioid 心脏形曲线 (整理)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 package demo;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5
6 import javax.swing.JFrame;
7 import javax.swing.JPanel;
8
9 /**
10 * Java Cardioid 心臟形曲線 (整理)
11 * 這是以前看到有關Cardioid一個故事之后,覺得好玩,那時把它實現了。
12 *
13 * 2016-1-2 深圳 南山平山村 曾劍鋒
14 */
15 public class Cardioid extends JPanel{
16 /**
17 * <ol>
18 * <li>centerX:代表x軸中心點坐標
19 * <li>centerY:代表y軸中心店坐標
20 * </ol>
21 * 整個窗口的長、寬等于2*centerX、2*centerY,這么做的目的是保證數據的統一性修改
22 * 當然你也可以考慮使用長、寬來表示,然后對他們進行取一半得到中心店坐標。
23 *
24 */
25 static int centerX = 600/2;
26 static int centerY = 600/2;
27 /**
28 * angle:主要用于角度計算,下面程序中的for循環里的變量,360一個周期
29 */
30 int angle;
31 /**
32 * <ol>
33 * <li>a:窗口中有2個心形圖,a是其中一個的幅值
34 * <li>b:窗口中有2個心形圖,b是其中一個的幅值
35 * </ol>
36 * 2個心形圖,2個幅值,但初始值是不一樣的。
37 */
38 int a = 0;
39 int b = 50;
40 /**
41 * 構造函數調用start()函數。<br>
42 * 作用:開啟一個線程,主要用于調整a、b的值,并刷新界面。
43 */
44 public Cardioid() {
45 start();
46 }
47 /**
48 * 重寫paint()<br>
49 * 程序流程:<ol>
50 * <li>調用父類paint方法、并設置背景顏色為黑色
51 * <li>用for循環畫兩個發散的心形圖
52 * </ol>
53 */
54 @Override
55 public void paint(Graphics graphics) {
56 super.paint(graphics);
57 this.setBackground(Color.black);
58 graphics.setColor(Color.red);
59 /*
60 * 這里是難點,主要是因為需要對坐標進行定位,下面是獲取x、y的坐標公式,你可以在網上查到
61 * x=a*(2*sin(t)-sin(2*t))
62 * y=a*(2*cos(t)-cos(2*t))
63 * 這里的x、y和網上的公式對調了,主要是因為需要進行y=x對稱,網上的圖是橫著的,這個圖是正著的。
64 * sin()函數傳入的是弧度制,所以需要通過angle*Math.PI/180,將角度值換成幅度值
65 * 其中的500主要是用于坐標調整的,沒有理由,是我自己試出來的,我也沒有去深究為什么,因為功能完成了。
66 */
67 for (angle = 0; angle < 360; angle++) {
68 graphics.drawLine(
69 centerY+(int)(a*(2*Math.sin(angle*Math.PI/180)-Math.sin(2*angle*Math.PI/180))),
70 500 -(centerX+(int)(a*(2*Math.cos(angle*Math.PI/180)-Math.cos(2*angle*Math.PI/180)))),
71 centerY+(int)((a+3)*(2*Math.sin((angle)*Math.PI/180)-Math.sin(2*(angle)*Math.PI/180))),
72 500 -(centerX+(int)((a+3)*(2*Math.cos((angle)*Math.PI/180)-Math.cos(2*(angle)*Math.PI/180)))));
73 }
74 for (angle = 0; angle < 360; angle++) {
75 graphics.drawLine(
76 centerY+(int)(b*(2*Math.sin(angle*Math.PI/180)-Math.sin(2*angle*Math.PI/180))),
77 500 -(centerX+(int)(b*(2*Math.cos(angle*Math.PI/180)-Math.cos(2*angle*Math.PI/180)))),
78 centerY+(int)((b+3)*(2*Math.sin((angle)*Math.PI/180)-Math.sin(2*(angle)*Math.PI/180))),
79 500 -(centerX+(int)((b+3)*(2*Math.cos((angle)*Math.PI/180)-Math.cos(2*(angle)*Math.PI/180)))));
80 }
81 }
82 /**
83 * 創建一個匿名線程,線程主要完成以下事情:<ol>
84 * <li>改變a、b的值,相當于改變心形線的幅值;
85 * <li>延時20ms;
86 * <li>刷新界面repaint();
87 * </ol>
88 */
89 public void start() {
90 new Thread(new Runnable() {
91
92 @Override
93 public void run() {
94 while (true) {
95 try {
96 if (a++ >100) {
97 a = 0;
98 }
99 if (b++ >100) {
100 b = 0;
101 }
102 Thread.sleep(20);
103 repaint();
104 } catch (InterruptedException e) {
105 e.printStackTrace();
106 }
107 }
108
109 }
110 }).start();
111 }
112 /**
113 * 主函數完成以下內容:<ol>
114 * <li>初始化jframe窗口;
115 * <li>創建cardioid,并將cardioid填充到jFrame中;
116 * <li>設置jFrame可見。
117 * </ol>
118 */
119 public static void main(String[] args) {
120 JFrame jFrame = new JFrame();
121 jFrame.setTitle("Cardioid");
122 jFrame.setSize(centerX*2, centerY*2);
123 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124 jFrame.setLocationRelativeTo(null);
125
126 Cardioid cardioid = new Cardioid();
127 jFrame.add(cardioid);
128 jFrame.setVisible(true);
129 }
130 }
?
轉載于:https://www.cnblogs.com/zengjfgit/p/5094721.html
總結
以上是生活随笔為你收集整理的Java Cardioid 心脏形曲线 (整理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 傲笑九天志在必得,卧薪尝胆勇者无畏.
- 下一篇: Linux系统简介 、 安装Linux系