- 1、本文档共3页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
小球碰撞实验
撞检测
在屏幕上的随机位置上出现小球,完成弹球的功能,与四周进行碰撞检测import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
public class LuckyBollCanvas extends Canvas{
private int x;//球的x坐标
private int y;//球的y坐标
private int offx, offy;//球跳跃的步长
public boolean pause;//控制是否结束
public LuckyBollCanvas(){ offx = offy = 10;
x = (this.getWidth() + offx) / 2;
y = (this.getHeight() + offy) / 2;
pause = false;
this.addCommand(new Command(Start , Command.OK, 1));
this.addCommand(new Command(Stop , Command.BACK, 1));
this.setCommandListener(new CanvasCommand());
}
protected void paint(Graphics arg0) { //刷新画板
arg0.setColor(255, 255, 255);
arg0.fillRect(0, 0, this.getWidth(), this.getHeight());
//画球
arg0.setColor(0, 0, 0);
arg0.fillArc(x, y, 0, 10, 0, 360);
}
//线程内部类
class LuckyBollThread implements Runnable{ public LuckyBollThread() {
super();
new Thread(this).start()
}
public void run(){
while (pause){
x += offx;
y += offy;
if (x 0){
offx = 10;
x = 0;
} else if (x getWidth() - offx) {
x = getWidth() - offx;
offx = -10;
}
if (y 0) {
offy = 10;
y = 0;
} else if (y getHeight() - offy) {
y = getHeight() - offy;
offy = -10;
}
repaint();
serviceRepaints();
try{
Thread.sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
//Command内部类
文档评论(0)