关于J2ME弹球代码的问题?
来源:百度知道 编辑:UC知道 时间:2024/06/17 00:20:50
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics ;
public class test1 extends Canvas implements Runnable{
int y=getHeight()/2-35;
boolean down=true;
public test1() {
new Thread(this).start();
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,99,89);
g.fillArc(getWidth()/2-35,y,70,70,0,360);
}
public void run() {
while(true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
if(down)
y++;
else
y--;
if(y>=getHeight()-70)
down=false;
if(y<=0)
down=true;
repaint();
}
}
}
}
应该是一个小球上下弹,不知为何不动?有明白的吗?
import javax.microedition.lcdui.Graphics ;
public class test1 extends Canvas implements Runnable{
int y=getHeight()/2-35;
boolean down=true;
public test1() {
new Thread(this).start();
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,99,89);
g.fillArc(getWidth()/2-35,y,70,70,0,360);
}
public void run() {
while(true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
if(down)
y++;
else
y--;
if(y>=getHeight()-70)
down=false;
if(y<=0)
down=true;
repaint();
}
}
}
}
应该是一个小球上下弹,不知为何不动?有明白的吗?
while(true){
if(down)
y++;
else
y--;
if(y>=getHeight()-70)
down=false;
if(y<=0)
down=true;
repaint();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
线程问题...你将if写在异常抛出中了...改一下