⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffertm.java

📁 简单的重力模拟试验和双缓冲绘图试验
💻 JAVA
字号:
/**
 * @(#)BufferTM.java
 *
 * 简单的重力模拟试验和双缓冲绘图试验
 *
 * @author 刘彬彬
 * @version 1.00 06/03/18
 */
package buffertm;

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

public class BufferTM extends JApplet implements Runnable {
	
	int x = 80;
	int y = 50;
	int r = 20;
	double o = 50; //下落的起点,每次都变化
	double t = 0;
	double v=0; //小球瞬时速度
	double v0=0; //上升时的初速度
	boolean touchGround=false; //判断是否触地
	boolean touchTop=false; //判断是否达到最高点
	boolean up=false; //判断是上升还是下降
	final double h=4.5; //地面坐标
	final double g = 0.3; //重力加速度(有比例尺的)

	private Image bgImage;
	private Graphics bg;
	
	public void testSituation() { //检验状态的方法
		if(v<0) touchTop=true;
		if(y>450) touchGround=true;
		if(touchTop) {
			touchTop=false;
			up=false;
			o=y;
			t=0;
		}
		if(touchGround) {
			v=v-2.8;
			v0=v;
			touchGround=false;
			up=true;
			t=0;
		}
	}
	
	public void move() { //小球移动的方法
		if(!up) {
			v=g*t;
			y=(int)(o+0.5*g*Math.pow(t,2));
			System.out.println("down"); //此行为测试用,无意义
		}
		if(up) {
			v=v0-g*t;
			y=(int)(h*100-v0*t+0.5*g*Math.pow(t,2));
			System.out.println("up"); //此行为测试用,无意义
		}
	}
	
	public void init() {
	}

	public void start() 
	{
		Thread th = new  Thread(this);
		th.start();
	}
	
	public void run()
	{
		int i=0;
		while(i++<450)
		{
			t++;
			testSituation();
			move();
			repaint();
			System.out.println(y); //此行为测试用,无意义
			
			try
			{
				Thread.sleep(1); //间隔为1毫秒
			}
			catch (InterruptedException e)
			{}
		}
	}

	public void stop() {}
	public void destory() {}

	public void update(Graphics g)
	{
		if (bgImage == null)
		{
			bgImage = createImage(this.getSize().width,this.getSize().height);
			bg = bgImage.getGraphics();
		}

		// 后台清屏,即设置圆球组件和后台一样的颜色,大小
		bg.setColor(getBackground());
		bg.fillRect(0,0,this.getSize().width,this.getSize().height);

		// 后台绘画
		bg.setColor(getForeground());
		paint(bg);

		g.drawImage(bgImage,0,0,this);
	}

	public void paint(Graphics g) {
		// 设置圆球的颜色
		g.setColor(Color.blue);		
		g.fillOval(x,y,2*r,2*r);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -