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

代码下载地址:http://www.zuidaima.com/share/1852345677564928.htm

原文:java swing实现小球沿正弦曲线运动的代码

package com.zuidaima.swing;

import java.awt.*;

import javax.swing.*;

public class SinRun extends JFrame implements Runnable {
	static int i = 0;
	static int j = 250;
	static double x = 0;
	static double v = 10;// 速度
	static double w = 2 * Math.PI;
	static double A = 50;// 振幅
	static double t = 0;// 时间

	public SinRun() {
		this.setSize(500, 500);
		this.setVisible(true);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.black);
		g.fillOval(i, j + (int) x, 10, 10);
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// e.printStackTrace();
			}
			i += v;
			x = A * Math.cos(w * t);
			t += 0.1;
			this.repaint();
			if (i > 500)
				i = 0;
		}
	}

	public static void main(String args[]) {
		new Thread(new SinRun()).start();
	}
}

java swing实现小球沿正弦曲线运动的代码_第1张图片

你可能感兴趣的:(java,swing,曲线运动,正弦)