求一段J2ME的代码?

我在J2ME模拟器上做了一个程序,一个方块从左上角向右下角移动,但是会超出屏幕,超出去后程序继续就是没有方块了,只剩背景,我想让方块碰到屏幕边上反弹这样无限反弹,应该加什... 我在J2ME模拟器上做了一个程序,一个方块从左上角向右下角移动,但是会超出屏幕,超出去后程序继续就是没有方块了,只剩背景,我想让方块碰到屏幕边上反弹这样无限反弹,应该加什么代码(我需要具体代码),是不是应该加在方块移动的代码后面? 展开
 我来答
抽烟的小孩儿
2011-07-23 · 超过18用户采纳过TA的回答
知道答主
回答量:25
采纳率:0%
帮助的人:45.7万
展开全部
给你一段参考吧 只不过是圆形的物体在屏幕上碰到墙后反弹 原理都一样 而且是用swt做的,不是j2me 你参考一下吧
public class Ball {
/** 球的半径 */
int diameter;
/** 最大半径 */
private final int maxR = 60;
/** 最大半径 */
private final int minR = 2;
/** 球的大小 */
public Rectangle bounds; // Bounding rectangle for the ball
/** 球的xy速度 */
Point vector; // The direction the ball is traveling

/** 球的颜色 */
Color c;

// 速度
private final int maxSpeed = 20;

private MyCanvas canvas;

/** 向量 */
VectorQuantity quantity;

private int timer;
private int timerMax = 6;

public void checkWallCollision() {
// Get the Client Rectangle
Rectangle cr;
cr = canvas.getBounds();
// Check for collision with Top Wall
if (bounds.y <= cr.y) {
// Reverse the Y vector
vector.y = -vector.y;

// Reset the bounding rectangle so the ball never appears outside of
// the screen
bounds.y = cr.y + 1;
bounds.height = bounds.y + diameter;
}
// Check for collision with Bottom Wall
else if (bounds.height >= cr.height) {
// Reverse the Y vector
vector.y = -vector.y;

// Reset the bounding rectangle so the ball never appears outside of
// the screen
bounds.height = cr.height - 1;
bounds.y = bounds.height - diameter;
}
// Check for collision with Left Wall
else if (bounds.x <= cr.x) {
// Reverse the X vector
vector.x = -vector.x;

// Reset the bounding rectangle so the ball never appears outside of
// the screen
bounds.x = cr.x + 1;
bounds.width = bounds.x + diameter;
}
// Check for collision with Right Wall
else if (bounds.width >= cr.width) {
// Reverse the X vector
vector.x = -vector.x;

// Reset the bounding rectangle so the ball never appears outside of
// the screen
bounds.width = cr.width - 1;
bounds.x = bounds.width - diameter;
}
}
天天哇乐
2011-07-22
知道答主
回答量:13
采纳率:0%
帮助的人:10.3万
展开全部
可以用if。代码如下:
//x,y为方块坐标,w为反弹时x坐标。等于屏宽减方形边长
while(true){
Boolean ft=false;
if(ft){
x--;
y--;
}else{
x++;
y++;
}
if(x>w || x<0){
ft=!ft;
//当满足反弹条件时,变换ft的值
}
repaint();
}
追问
public void run() {
while (true) {
try {
Thread.sleep(5);
} catch (Exception ex) {

}
repaint();
x++;
y++;
}
}
这是我的代码,应该加在哪里怎么加
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
是田明启
2011-07-23
知道答主
回答量:41
采纳率:0%
帮助的人:23.1万
展开全部
学习是要用脑子的,
我在表扬你好问的同事,再批评你,凡事用心想想,
你可以实现向右下移动,就不能让他返回来?你懒的可以,或都说*的可以
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式