我現在實現了小球撞墻,要想實現小球在運動過程中把運動軌跡畫出來應該怎么修改代碼?求高手解答,以下是現在的代碼importjava.awt.*;publicclassMyTest3{publicstaticvoidmain(Strin...
我現在實現了小球撞墻,要想實現小球在運動過程中把運動軌跡畫出來應該怎么修改代碼?求高手解答,以下是現在的代碼
import java.awt.*;
public class MyTest3 {
public static void main(String[] args) {
Frame w = new Frame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
Thread t = new Thread(mp);
t.start();
w.show();
}
}
class MyPanel extends Panel implements Runnable {
int x = 30;
int y = 30;
int att = 1;
public void paint(Graphics g) {
g.fillOval(x, y, 30, 30);
}
public void run() {
while(true) {
//定義小球飛行姿態
if(att == 1) {
x ++ ;
y ++ ;
}
if (att == 2) {
x -- ;
y ++ ;
}
if (att == 3) {
x -- ;
y -- ;
}
if (att == 4) {
x ++ ;
y -- ;
}
//改變小球的飛行姿態
if (x > 253) {
if(att == 1) {
att = 2;
} else {
att = 3;
}
}
if (y > 333) {
if (att == 2) {
att = 3;
} else {
att = 4;
}
}
if (x < 0) {
if(att == 3) {
att = 4;
} else {
att = 1;
}
}
if (y < 0) {
if(att == 4) {
att = 1;
} else {
att = 2;
}
}
try {
Thread.sleep(10);
} catch(Exception e) {
}
repaint();
}
}
}
展開
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态