首页 > 代码库 > Java_Swing实现小球沿正弦曲线运动的代码

Java_Swing实现小球沿正弦曲线运动的代码


 1 package zuidaimapack_1;
 2 import java.awt.*;
 3 import javax.swing.*;
 4 /**
 5  *Java_Swing实现小球沿正弦曲线运动的代码
 6  * @author Visec·Dana
 7  */
 8 public class SinRun extends JFrame implements Runnable {
 9     private static final long serialVersionUID = 1L;
10     static int i = 0;
11     static int j = 250;
12     static double x = 0;
13     static double v = 5;// 速度
14     static double w = 2 * Math.PI;
15     static double A = 50;// 振幅
16     static double t = 0;// 时间
17  
18     public SinRun() {
19         this.setSize(500, 500);
20         this.setVisible(true);
21     }
22  
23     public void paint(Graphics g) {
24         super.paint(g);
25         g.setColor(Color.black);
26         g.fillOval(i, j + (int) x, 10, 10);
27     }
28     public void run() {
29         while (true) {
30             try {
31                 Thread.sleep(100);
32             } catch (InterruptedException e) {
33                 // e.printStackTrace();
34             }
35             i += v;
36             x = A * Math.sin(w * t); 
37             t += 0.1;
38             this.repaint();
39             if (i > 500)
40                 i = 0;
41         }
42     }
43     public static void main(String args[]) {
44         new Thread(new SinRun()).start();
45     }
46 }