JAVA小球自由落体运动钢性碰撞程序
来源:优易学  2011-12-21 16:48:23   【优易学:中国教育考试门户网】   资料下载   IT书店

  import java.awt.BorderLayout;

  import java.awt.Color;

  import java.awt.Component;

  import java.awt.Container;

  import java.awt.Graphics;

  import java.awt.Graphics2D;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.image.BufferedImage;

  import javax.swing.JFrame;

  import javax.swing.Timer;

  public class BallJump /*extends JFrame*/ extends JFrame implements ActionListener{

  private static final int SIZER = 20;

  private static  int DELAY = 30;

  private static final int SIZEX = 200;

  private static final int SIZEY = 560;

  JFrame f;

  int x=200,y=0;

  double g=9.8;

  int t=2;

  private double px=20;

  private double py;

  private double sx;

  private double sy;

  private BufferedImage bm2;

  Timer timer;

  MyPanel p1;

  boolean flag=true;

  int a;

  BallJump(){

  JFrame.setDefaultLookAndFeelDecorated(true);

  bm2 = new BufferedImage(SIZER * 2, SIZER * 2,BufferedImage.TYPE_4BYTE_ABGR);

  timer = new Timer(DELAY, this);

  timer.setInitialDelay(0);

  timer.setCoalesce(true);

  initBm2(bm2);

  //f = new JFrame();

  p1=new MyPanel();

  add(p1,BorderLayout.CENTER);

  this.pack();

  setBounds(200, 100, 200, 590);

  //this.setLocation(200,100);

  this.setResizable(false);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  private void initBm2(BufferedImage bm2) {

  Graphics2D g2 = bm2.createGraphics();

  int step = 255 / SIZER;

  for (int i = 0; i < SIZER; i++) {

  g2.setColor(new Color((255 - i * step) / 2, (i * step) / 2 + 127,255 - i * step));

  g2.fillArc(i, i, (SIZER - i) * 2, (SIZER - i) * 2, 0, 360);

  }

  }

  public void actionPerformed(ActionEvent e) {

  if(flag && this.getHeight()-py>=90 ){

  py+=9.8*t*t/2;

  System.out.println(py);

  }else {

  flag=false;

  if(py>=500)

  py-=200;

  else if(py>500 && py<=200)

  py-=40;

  else if(py>200 && py<=100)

  py-=20;

  else

  py-=5;

  if(py<=0){

  flag=true;

  }

  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训