【JAVA】重力模拟

import javax.swing.*;
import java.awt.*;

class Base
{
    private int x;
    private int y;
    private int displayWidth;
    private int displayHeight;
    private final Image image;

    public Base(int x, int y, int displayWidth, int displayHeight, String fileName) {
        this.x = x;
        this.y = y;
        this.displayWidth = displayWidth;
        this.displayHeight = displayHeight;
        this.image = Toolkit.getDefaultToolkit().getImage( "resources\\images\\" + fileName);
    }

    Base(Image image) {
        this.image = image;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDisplayWidth() {
        return displayWidth;
    }

    public void setDisplayWidth(int displayWidth) {
        this.displayWidth = displayWidth;
    }

    public int getDisplayHeight() {
        return displayHeight;
    }

    public void setDisplayHeight(int displayHeight) {
        this.displayHeight = displayHeight;
    }

    public Image getImage() {
        return image;
    }
}

class Bullet extends Base
{
    public Bullet(int x, int y, int displayWidth, int displayHeight, String fileName)
    {
        super(x, y, displayWidth, displayHeight, fileName);
    }
}


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
     */
    @Override
    public 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();
    }
}

运行效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值