package gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 编写一个Java GUI应用程序,采用Java多线程技术,有两个线程,模拟垂直上抛运动和水平抛体运动:一个球垂直上抛,一个球水平抛出。(本题30分) (垂直上抛物理公式:h=v0*t-g*t2/2 ;平抛运动物理公式:h=g*t*2/2 ,x=v*t ; h代表高度,v0代表初速度=30 m/s ,t代表时间,g代表重力加速度=9.8 m/s2 ,v代表平抛速度=30 m/s ) @start 2010-9-19 11:40 @end 12:20 * */ public class Two_TwoBallTwo { public static void main(String[] args){ TwoBallTwo ball=new TwoBallTwo(); ball.init(); ball.start(); } } class TwoBallTwo extends JFrame{ JRadioButton ball1=new JRadioButton(); JRadioButton ball2=new JRadioButton(); double g=9.8; double v0=30; double t=1; double t2=1; double t3=1; int width=800; int height=600; public void init(){ this.getContentPane().add(ball1); this.getContentPane().add(ball2); ball1.setBounds(50, height/2,20 ,20); ball2.setBounds(5,10,20,20); ball1.setSelected(true); ball2.setSelected(true); this.validate(); this.setVisible(true); this.setBounds(111, 111, 800, 600); } public void start(){ //上 抛 --加修 正 Thread thread1=new Thread(){ public void run(){ while(true){ //side area confirm if(Math.abs(ball1.getX())<width&&Math.abs(ball1.getY())<height){ t3+=0.1; if(t<1){ t+=0.1; } ball1.setBounds(ball1.getX(),(int)(ball1.getY()-(v0*t-g*t3*t3/2)), 20, 20); } else{ try{ this.sleep(1000); } catch(Exception e2){} t3=1; ball1.setBounds(50, height/2,20 ,20); t=1; } try{ this.sleep(30); } catch(Exception e){} } } }; //平 抛 --add修 正 Thread thread2=new Thread(){ public void run(){ while(true){ //side area confirm if(Math.abs(ball2.getX())<width&&Math.abs(ball2.getY())<height){ t2+=0.2; ball2.setBounds((int)(ball1.getX()*t2),(int)(g*t2*t2/2), 20, 20); } else{ try{ this.sleep(30); } catch(Exception e2){} t2=1; ball2.setBounds(5, 10,20 ,20); } try{ this.sleep(30); } catch(Exception e){} } } }; thread1.start(); thread2.start(); } }