关于小球的自由落体运动程序大家首先要知道自由落体运动包括水平的运动和垂直的运动 ,水平的运动=vt 垂直运动=1/2t2*9.8 然后我们先定义一个小球的类 |
package Itat;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame implements Runnable {
private Thread vercail,cuizi;//两条线程
private Ball ball1,ball2;//两个球
public MyFrame() {
// 初始窗口的属性
this.setBounds(0, 0, 400, 400);
this.setLayout(null);
this.setVisible(true);
this.addWindowListener(new event());
}
public void start_cuizi(){
cuizi=new Thread(this);
cuizi.start();
}
public void start_pingpao(){
vercail=new Thread(this);
vercail.start();
}
private Thread vercail,cuizi;//两条线程
private Ball ball1,ball2;//两个球
public MyFrame() {
// 初始窗口的属性
this.setBounds(0, 0, 400, 400);
this.setLayout(null);
this.setVisible(true);
this.addWindowListener(new event());
}
public void start_cuizi(){
cuizi=new Thread(this);
cuizi.start();
}
public void start_pingpao(){
vercail=new Thread(this);
vercail.start();
}
// 垂直运动
public void vercial() {
Ball blackball=new Ball(15,Color.black);
this.add(blackball);
int t = 0;
while (true) {
t++;
if (t > 20)
t=0;
int x = 50;// 横坐标
int h = (int) (1.0 / 2 * t * t * 9.8) + 50;// 高度根据时间变化
blackball.setLocation(x, h);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void vercial() {
Ball blackball=new Ball(15,Color.black);
this.add(blackball);
int t = 0;
while (true) {
t++;
if (t > 20)
t=0;
int x = 50;// 横坐标
int h = (int) (1.0 / 2 * t * t * 9.8) + 50;// 高度根据时间变化
blackball.setLocation(x, h);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class event extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
// 平抛运动
public void level() {
Ball redball=new Ball(15,Color.black);
this.add(redball);
int t = 0;
while (true) {
t++;
if (t > 20)
t=0;
int x = 50 + t * 30;
int h = (int) (1.0 / 2 * t * t * 9.8) + 50;
redball.setLocation(x, h);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void level() {
Ball redball=new Ball(15,Color.black);
this.add(redball);
int t = 0;
while (true) {
t++;
if (t > 20)
t=0;
int x = 50 + t * 30;
int h = (int) (1.0 / 2 * t * t * 9.8) + 50;
redball.setLocation(x, h);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void run() {
if(Thread.currentThread()==cuizi){
vercial();
}
else if(Thread.currentThread()==vercail){
level();
}
if(Thread.currentThread()==cuizi){
vercial();
}
else if(Thread.currentThread()==vercail){
level();
}
}
// 主函数
public static void main(String args[]) {
// // 定义一个半径为15的红色球
// Ball ball = new Ball(15, Color.red);
// Ball ball1 = new Ball(15, Color.red);
// // 定义一个窗口
// MyFrame myframe = new MyFrame();
// MyFrame myframe1 = new MyFrame();
// // myframe.vercial(ball);
// myframe1.level(ball1);
// MyFrame myframe1 = new MyFrame(1);
// // Ball ball1=new Ball(15, Color.red);
// Thread th1 = new Thread(myframe1);
// th1.start();
// // MyFrame myframe2 = new MyFrame(2);
// Thread th2 = new Thread(myframe1);
// th2.start();
MyFrame myframe=new MyFrame();
myframe.start_cuizi();
myframe.start_pingpao();
}
}
public static void main(String args[]) {
// // 定义一个半径为15的红色球
// Ball ball = new Ball(15, Color.red);
// Ball ball1 = new Ball(15, Color.red);
// // 定义一个窗口
// MyFrame myframe = new MyFrame();
// MyFrame myframe1 = new MyFrame();
// // myframe.vercial(ball);
// myframe1.level(ball1);
// MyFrame myframe1 = new MyFrame(1);
// // Ball ball1=new Ball(15, Color.red);
// Thread th1 = new Thread(myframe1);
// th1.start();
// // MyFrame myframe2 = new MyFrame(2);
// Thread th2 = new Thread(myframe1);
// th2.start();
MyFrame myframe=new MyFrame();
myframe.start_cuizi();
myframe.start_pingpao();
}
}
// 定义一个球
class Ball extends Panel {
class Ball extends Panel {
private int recial;// 圆的半径
private Color c;// 球的颜色
private Color c;// 球的颜色
public Ball(int recial, Color c) {
this.recial = recial;
this.c = c;
this.setSize(recial * 2, recial * 2);
}
this.recial = recial;
this.c = c;
this.setSize(recial * 2, recial * 2);
}
public void paint(Graphics g) {
g.setColor(this.c);
g.fillOval(0, 0, recial * 2, recial * 2);
}
g.setColor(this.c);
g.fillOval(0, 0, recial * 2, recial * 2);
}
// public void run() {
// // TODO Auto-generated method stub
//
// }
// // TODO Auto-generated method stub
//
// }
}
转载于:https://blog.51cto.com/2166183/461907