J2ME 新手求教
颍川公子 2010-09-24 08:16:02 请大家帮忙看看以下代码,如何实现再增加一个球使这两个球同时运动,一个使之沿着屏幕运动,另一个沿着斜线运动并会反弹。 请高手赐教
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class NewClass extends Canvas implements Runnable {
int width;
int height;
int x;
int y;
int v1 = 5;
int v2 = 5;
int x1;
int y1;
public NewClass() {
//获得屏幕的宽高
width = this.getWidth();
height = this.getHeight();
new Thread(this).start();
}
protected void paint(Graphics g) {
//清理屏幕
g.setColor(0, 255, 0);
g.fillRect(0, 0, width, height);
g.setColor(255, 0, 0);
g.fillArc(x, y, 10, 10, 0, 360);
void update() {
x = x + v1;
y = y + v2;
if (x >= this.getWidth() - 10) {
v1 = -v1;
} else if (y >= this.getHeight() - 10) {
v2 = -v2;
} else if (x <= 0) {
v1 = -v1;
} else if (y <= 0) {
v2 = -v2;
}
}
void second() {
x = x + v1;
if (x >= this.getWidth() - 10) {
v1 = 0;
v2 = 5;
y = y + v2;
}else if (y >= this.getHeight() - 10) {
v2 = 0;
v1 = -5;
x = x+v1;
}else if(x == 0){
v1 = 0;
v2 = -5;
y+=v2;
}else if(y == 0){
v2 = 0;
v1 = 5;
x+=v1;
}
}
public void run() {
while (true) {
try {
update();
//second();
repaint();
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}