Java多线程实现同时进行小球的自由落体与平抛

Java多线程的实现方法有继承Thread和实现接口Runnable。我这里用的是通过实现接口Runnable来创建新线程的。

要实现自由落体与平抛。

首先,是小球在窗口中运动。将小球封装成一个类,继承Canvas。通过new小球的类,添加到窗口上。

运动的是不断的变化小球的位置,通过Sleep一小段时间。来实现动画的效果。


下面的是代码,有详细注释。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BallDemo
{
	public static void main(String[] args)
	{
		Movement m = new Movement();
		m.setResizable(false);  //设置窗口大小不可变化
	}
}
class Movement extends JFrame implements Runnable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton button;
	Thread pinkball, yellowball;
	Ball pink, yellow;
	double time = 0.0;
	Movement()
	{
		super("Ball Show");             //窗口标题
		setBounds(300, 200, 600, 400);  //设置窗口位置和大小
		setLayout(null);                //空的布局管理器,里面的部件位置可以根据自己的想法放在任意位置
		setVisible(true);               //设置可见
		button = new JButton("Start");  //创建一个按钮
		button.setBounds(530, 0, 65, 30);//设置按钮位置和大小
		add(button);                     //添加到窗口上
		pinkball = new Thread(this);     //创建线程,里面加的是this,因为这个类实现了接口Runnable
		yellowball = new Thread(this);   //创建线程
		pink = new Ball(Color.pink);     //创建粉色小球
		yellow = new Ball(Color.yellow); //创建黄色小球
		add(pink);                       //添加到窗口上
		add(yellow);                     //添加到窗口上
		pink.setLocation(150, 20);       //设置显示的位置
		yellow.setLocation(300, 20);     //设置显示的位置
		button.addActionListener(new ActionListener()  //按钮添加活动事件监听,一旦按下按钮,就执行下面的函数
				{
					public void actionPerformed(ActionEvent e)    //开启线程
					{
						pinkball.start();
						yellowball.start();
					}});
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //设置点击右上角的关闭窗口时  关闭窗口。
	}
	public void run()   //run方法
	{
		while(true)
		{
			time += 0.2;   //时间递增
			if(time > 20)  //超过了20,重新来过
				time = 0.0;
			
			if(Thread.currentThread() == pinkball)  //粉色的球的线程
			{
				int x = 150;
				int y =  (int)(1.0 / 2 * time * time * 9.8) + 20;   //通过1/2gt^2 + 原始位置上的y算出之后的位置
				pink.setLocation(x, y);   //设置位置
				try
				{
					Thread.sleep(50);    //线程睡眠50MS
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
			if(Thread.currentThread() == yellowball)
			{
				int x = 300 + (int)(30 * time);         //通过s = vt公式加上原始位置的x算出之后的位置
			    <span style="white-space:pre">	</span>int y = (int)(1.0 / 2 * time * time * 9.8) + 20;  //<span style="font-family: Arial, Helvetica, sans-serif;">通过1/2gt^2 + 原始位置上的y算出之后的位置</span>
				yellow.setLocation(x, y);     //设置位置
				try
				{
					Thread.sleep(50);    //线程睡眠50MS
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}
class Ball extends Canvas    //Ball 类,继承了Canvas,
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;
	Color color;
	public Ball(Color color)    //通过构造函数设置好大小和颜色
	{
		this.color = color;
		setSize(20, 20);
	}
	public void paint(Graphics g)    //画出一个圆形。
	{
		g.setColor(color);
		g.fillOval(0, 0, 20, 20);
	}
}

这里只放一张截图,不会弄GIF动态图。



  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,模拟小自由落体运动通常涉及到基本的物理学原理和简单的动画或游戏开发。这里是一个简化的示例,使用`java.awt`库来创建一个二维图形中的自由落体动画: ```java import java.awt.*; import javax.swing.*; public class FallingBall extends JFrame { private static final int BALL_RADIUS = 10; private static final int UPDATE_INTERVAL = 50; // 每隔多少毫秒更新一次 private static final int SCREEN_WIDTH = 800; private static final int SCREEN_HEIGHT = 600; private Ball ball; public FallingBall() { ball = new Ball(BALL_RADIUS, 0, SCREEN_HEIGHT / 2); // 初始位置为屏幕中心 setSize(SCREEN_WIDTH, SCREEN_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); setIgnoreRepaint(true); Timer timer = new Timer(UPDATE_INTERVAL, (e) -> { update(); repaint(); }); timer.start(); } private void update() { ball.updateGravity(); // 假设ball有一个updateGravity方法来处理加速度 if (ball.getY() + ball.getRadius() > SCREEN_HEIGHT) { // 如果超过屏幕顶部,重置位置 ball.setY(0); ball.setX((int) (Math.random() * (SCREEN_WIDTH - 2 * BALL_RADIUS))); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.fillOval(ball.getX(), ball.getY(), BALL_RADIUS * 2, BALL_RADIUS * 2); // 绘制小 } public static void main(String[] args) { new FallingBall(); } class Ball { private int x, y; private int radius; private double gravity = 9.8; // 重力加速度 public Ball(int radius, int x, int y) { this.radius = radius; this.x = x; this.y = y; } public void updateGravity() { y -= gravity * UPDATE_INTERVAL / 1000.0; // 将时间间隔转换为秒 } // 其他获取坐标的方法... @Override public String toString() { return "Ball at (" + x + ", " + y + ")"; } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值