本文主要是介绍【JAVA】重力模拟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import javax.swing.*;
import java.awt.*;public class GameFrame3 extends JFrame implements Runnable {private long timerClick;private final Graphics graphics; //画笔private final Bullet bullet;private int t ;private int v0;private int vt;private int g;public GameFrame3() {//画板的宽度int windowWidth = 629;//画板的高度int windowHeight = 990;setLayout(null);setSize(windowWidth, windowHeight);setLocationRelativeTo(null);setVisible(true);timerClick = 0;v0 = 100;g = 10;graphics = getContentPane().getGraphics();bullet = new Bullet(0, 600, 64, 64, "bullet_01.png");}/*** 速度-时间公式:v=gt* 位移-时间公式; h=gt^2/2* 速度-位移公式:v^2=2gh*/@Overridepublic void run() {while (true){timerClick++;graphics.drawImage(bullet.getImage(), bullet.getX(), bullet.getY(),bullet.getDisplayWidth(),bullet.getDisplayHeight(),this);if (timerClick % 100 == 0){t += 1;vt = v0 - g * t;int h = (int) (v0 * t -1 / 2.0 * g * t * t);bullet.setX(t * 10);bullet.setY(600 - h);System.out.println(bullet.getX() + " " + bullet.getY());}try {Thread.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}}}public static void main(String[] args) {GameFrame3 gameFrame3 = new GameFrame3();Thread thread = new Thread(gameFrame3);thread.start();}
}
运行效果:
这篇关于【JAVA】重力模拟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!