2010-07-21 94 views
0

请看看这段代码,当我在诺基亚N97上运行它时,它的运行速度非常慢,但是我测试它在三星corby上运行正常,我认为如果我使用游戏画布问题应该被缓解。我该怎么做才能解决这个问题。为什么此代码在某些设备上运行缓慢?

public class MIDPCanvas extends GameCanvas implements Runnable { 

Graphics g; 
Image img; 
int x = getWidth()/2; 
Thread t = new Thread(this); 

public MIDPCanvas() { 
    super(true); 

    try { 
     img = Image.createImage("/pic.jpg"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

    g = getGraphics(); 
    g.setColor(150, 230, 200); 
    g.fillRect(0, 0, getWidth(), getHeight()); 

    t.start(); 
} 

public void run() { 
    while (true) { 
     g.drawImage(img, x, getHeight()/2, Graphics.VCENTER | Graphics.HCENTER); 
     x--; 
     flushGraphics(); 
    } 
    } 
} 

感谢

回答

0

既然你有run()一个无限循环,我猜想,在对科比的while循环框,或该设备的呼叫一个莫名其妙节流的过程:

while (true) { 
     g.drawImage(...); 
     x--; 
     flushGraphics(); 
    } 
0

这是一个非常糟糕的做动画的方法。

如果您必须使用无限循环方法,则在每次迭代中至少包含yield()Thread.sleep(100)。但更好的方法是使用Display.callSerially()。这将确保设备的动画速度尽可能快。

谷歌是你的朋友解释所有关于如何使用这种方法。

个人而言,在多年的做用大量动画的高复杂性J2ME应用程序,我从来没用过GameCanvas - 总是Canvas,覆盖paint()方法和使用callSerially()动画。

+0

我改变我的代码 - 用帆布和的callSerially(),但现在当我运行它没有动! – mahdi 2010-07-24 21:18:04

0

这是新的代码,但没有招

public class MIDPCanvas extends Canvas implements Runnable { 

Graphics g; 
Image img; 
int x = getWidth()/2; 
Thread t = new Thread(this); 
Display display; 

public MIDPCanvas(Display display) { 

    this.display = display; 
    try { 
     img = Image.createImage("/pic.jpg"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

    t.start(); 
} 

public void run() { 
    while (true) { 
     x--; 
     repaint(); 
     display.callSerially(this); 
    } 
} 

protected void paint(Graphics g) { 
    g.drawImage(img, x, getHeight()/2, Graphics.VCENTER | Graphics.HCENTER); 
    display.callSerially(this); 
} 

}

+0

这是错误的;你不明白'callSerially()'是如何工作的。 'callSerially()'中的'Runnable'不是您用于实例化线程的常规Runnable。摆脱你的'线程t',摆脱'run()'方法中的无限循环。 'run()'方法应该只执行动画逻辑('x - ')和'repaint()'。 MIDP生命周期将在适当的时候调用'run()',然后触发重绘。 或者,您可以在'paint()'方法中执行动画逻辑。 – funkybro 2010-07-25 17:57:12

+0

您可能会发现,这会导致比预期快得多的动画,在这种情况下,您可以测量重新绘制之间的时间以限制速度。 – funkybro 2010-07-25 17:59:06

+0

我发现如何使用callSerially(),它的工作,但仍然是我的问题没有解决,它运行在wtk非常快,但在其他模拟器,如LG或诺基亚其慢 – mahdi 2010-07-25 22:04:43

相关问题