?? buffertm.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 + -