Canvas类画布范围问题....

liun_li liun_li 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
做了一个滚动的小球程序,碰到障碍物会自动弹回,但是小球会滚动到下图的灰色部分,也就是说,这也属于画布的范围,有什么办法,能准确计算出灰色部分的高度.或者,让画面范围止于灰色部分..

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.perseus.parser.ColorParser;

public class bool extends MIDlet {
Display dis;
Can ca;
public bool() {
dis=Display.getDisplay(this);
ca=new Can();
ca.setTitle("会滚动的小球");
dis.setCurrent(ca);
new Thread(ca).start();
}
protected void startApp() throws MIDletStateChangeException {

}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根

}

protected void pauseApp() {
// TODO 自动生成方法存根

}

class Can extends Canvas implements Runnable
{
int x=0,y=0;
int Width=this.getWidth();
int Heigth=this.getHeight();
int dx=8,dy=8;
final int r=7;
Can()
{
//setFullScreenMode(true);
}
void move()
{
x=x+dx;
y=y+dy;
if(x+r>=Width)
{
x=Width-r;
dx=-dx;
}
if(x-r<0)
{
x=0;
dx=-dx;
}
if(y+r>=Heigth)
{

y=Heigth-r;
dy=-dy;
}
if(y-r<0)
{
y=0;
dy=-dy;
}
}

protected void paint(Graphics g) {
g.setColor(250,250,250);
g.fillRect(0, 0, Width, Heigth);
move();
g.setColor(100,255,20);
g.fillRoundRect(x, y, r,r,90,90);
}

public void run() {
while(true)
{
repaint();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}

}

}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
liun_li
J2ME 13,096 成员 26,491 内容