日期:2014-05-20  浏览次数:20793 次

高手快来救命:我做了黑色和红色两个小球,想让它们同时运行,可就是不同时运行。
完整代码如下:


import   javax.microedition.midlet.MIDlet;
import   javax.microedition.lcdui.*;
import   java.util.*;
import   javax.microedition.midlet.MIDletStateChangeException;


public   class   MoveBall   extends   MIDlet   implements   CommandListener     {
private   Display   display;
private   Command   cmdExit;
Ball   ball;
RedBall   redball;
public   MoveBall()   {
//   TODO   自动生成构造函数存根
display=Display.getDisplay(this);
cmdExit=new   Command( "退出 ",Command.SCREEN,1);
ball=new   Ball(display);
ball.addCommand(cmdExit);
ball.setCommandListener(this);
redball=new   RedBall(display);
redball.addCommand(cmdExit);
redball.setCommandListener(this);
}

protected   void   destroyApp(boolean   arg0)   throws   MIDletStateChangeException   {
//   TODO   自动生成方法存根
ball.destroy();
//notifyDestroyed();
}

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

}

protected   void   startApp()   throws   MIDletStateChangeException   {
//   TODO   自动生成方法存根
ball.start();
redball.start();
}
public   void   commandAction(Command   c,Displayable   d)
{
if(c==cmdExit)
{
notifyDestroyed();
}
}

public   class   Ball   extends   Canvas   implements     Runnable   {
Random   random=new   Random();
Display   display;
int   posX=50;
int   posY=50;
int   ballsize=30;

public     Ball(Display   display)
{
super();
this.display=display;
}

/*public   void   run()
{
while(true)
{
                        this.posX=(random.nextInt()> > > 1)%(this.getWidth()-20)+10;
this.posY=(random.nextInt()> > > 1)%(this.getHeight()-20)+10;
try{
Thread.sleep(1000);
}
catch(Exception   e)
{
e.printStackTrace();
}
repaint();
}
}*/
public   void   run()
{

while(true)
{
this.posY   +=   10;
if(this.posY   >   this.getHeight())   {
posY   =   10;
}

try{
Thread.sleep(200);
}   catch   (Exception   e)   {
e.printStackTrace();
}
repaint();
}
}
  void   start()
{
//posX=(random.nextInt()> > > 1)%(this.getWidth()-20)+10;
//posY=(random.nextInt()> > > 1)%(this.getHeight()-20)+10;
display.setCurrent(this);
Thread   t=new   Thread(this);
t.start();
repaint();

}
  void   destroy()
{

}
public   void   paint(Graphics   g)
{
int   x=g.getClipX();
int   y=g.getClipY();
int   width=g.getClipWidth();
int   height=g.getClipHeight();
g.setColor(0xffffff);
g.fillRect(x,   y,   width,   height);
g.setColor(30);
g.fillArc(posX,   posY,   ballsize,   ballsize,   0,   360);
}

}

public   class   RedBall   extends   Canvas   implements   Runnable{