JAVA 自由落体和平抛无能运动

本文出处:http://blog.163.com/mr_liuyong/blog/static/123424376200994104952814/

JAVA 自由落体和平抛无能运动

编写一个Java GUI应用程序,采用Java多线程技术,模拟自由落体和平抛运动:一个球自由落下,一个球水平抛出。(本题30分)(自由落体物理公式:h= g *t2/2 ;平抛运动物理公式:h= g *t2/2 ,x=26*t ;
h代表高度,t代表时间,g代表重力加速度=9.8 m/s2

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;


public class DropTest extends Frame implements Runnable,ActionListener{
 double x1 = 100,y1 = 0;//A的起始位置
 double x2 = 100,y2 = 0;//B的起始位置
 double s2 = 26;//B的水平速度
 double g = 9.8;//G
 long time = 10000;//模拟10秒钟
 double py = 1;//y轴比例尺
 List<Point> list1 = new ArrayList<Point>();
 List<Point> list2 = new ArrayList<Point>();
 
 MyPanel p1 = new MyPanel();
 Button b1 = new Button("启动");
 Thread t ;
 public DropTest(){
  b1.addActionListener(this);
  this.add(b1,BorderLayout.NORTH);
  this.add(p1,BorderLayout.CENTER);
  this.setSize(800,600);
  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }});
  this.setVisible(true);
 }
 
 public void run(){
  list1 = new ArrayList<Point>();
  list2 = new ArrayList<Point>();
  double xx1 = x1,yy1 = y1,xx2 = x2,yy2 = y2;//本时刻位置
  long start = System.currentTimeMillis();
  long end = start;
  while(end-start<=time){
   end = System.currentTimeMillis();
   double t = (end - start)/1000.0;
   yy1 = (y1 + g*t*t/2)*py;
   xx2 = x1 + s2*t;
   yy2 = (y2 + g*t*t/2)*py; 
  
   int sx1 = (int)xx1;
   int sy1 = (int)yy1;
   int sx2 = (int)xx2;
   int sy2 = (int)yy2;
   list1.add(new Point(sx1,sy1));
   list2.add(new Point(sx2,sy2));
   p1.repaint();
   try {
    Thread.sleep(50);
   } catch (InterruptedException e) {  
    e.printStackTrace();
   }
  }
 }
 
 
 public void actionPerformed(ActionEvent e) {
  if (t==null||!t.isAlive()){
   t = new Thread(this);
   t.start();
  } 
 }


class MyPanel extends Panel{
  BufferedImage im = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
  public void paint(Graphics gg){
   Graphics g = im.getGraphics();
   g.setColor(Color.white);
   g.fillRect(0,0,800,600);
   g.setColor(Color.blue);
   Point ppp1 = null;
   Point ppp2 = null;
   for (int i=0;i<list1.size();i++){
    Point pp1 = list1.get(i);
    Point pp2 = list2.get(i);
    if (ppp1!=null&&ppp2!=null){
     g.drawLine(pp1.x,pp1.y,ppp1.x,ppp1.y);
     g.drawLine(pp2.x,pp2.y,ppp2.x,ppp2.y);
    }
    ppp1 = pp1;
    ppp2 = pp2;
   }
   if (ppp1!=null&&ppp2!=null){
    g.fillOval(ppp1.x-5,ppp1.y-5,10,10);
    g.fillOval(ppp2.x-5,ppp2.y-5,10,10);
   }
   g.dispose();
   gg.drawImage(im,0,0,800,600,p1);
  }
 }
 public static void main(String[] args){
  new DropTest();
 }
}

你可能感兴趣的:(java,thread,list,null,Class,button)