2012-03-31 83 views
0

我有一個包含Thread,MouseListerner和MouseMotionListener的Java Applet。Java Applet在移動鼠標時速度變慢

拖動鼠標時,在小應用程序上繪製一些東西,然後落下。 這確實有效,但是當鼠標移動而沒有按下時,落下的物體不會流利地移動。

線程調用重繪功能

while(running){ 
    repaint(); 
} 

的的mouseMoved函數是空的,來電的mouseDragged重繪。 我希望有人知道這個問題,謝謝

+1

1)'而(運行){重繪(); }'不要在沒有'wait()'的情況下做到這一點2)爲了更好地幫助,請發佈[SSCCE](http://sscce.org/)。 3)這個千年,使用Swing而不是AWT組件。 4)這可能應該是使用[Java Web Start](http://stackoverflow.com/tags/java-web-start/info)(而不是嵌入式小程序,這很棘手)從鏈接啓動的框架。 – 2012-03-31 16:41:49

回答

1

你正在佔用你的主線程的CPU(unnecesaruly)。既然你不需要更新速度加快,每40毫秒,可能每80就足夠了,你應該返工你的主循環是這樣的:

/** 
* internal game loop method. 
*/ 
public void updateGameState() { 
    lastFrameTime = System.currentTimeMillis(); 

    // as long as we run we move 
    while (state == GameState.RUNNING) { 
     currentFrame++; 
     timeNow = System.currentTimeMillis(); 

     // sleep until this frame is scheduled 
     long l = lastFrameTime + FRAME_DELAY - timeNow; 
     updatePositions(); 
     redraw(); 
     //System.err.println("............. delay:" + l); 
     if (l > 0L) { 
      try { 
       Thread.sleep(l); 
      } 
      catch (Exception exception) { 
      } 
     } else { 
      // something long kept us from updating, reset delays 
      lastFrameTime = timeNow; 
      l = FRAME_DELAY; 
     } 

     //System.err.println("lft: " + lastFrameTime + " tn: " + timeNow); 
     lastFrameTime = timeNow + l; 
     // be polite, let others play 
     Thread.yield(); 
    } 
    ///System.err.println("leave game loop"); 
} 
+0

謝謝你的回答,我會盡力實施它們 – user1305241 2012-04-01 10:03:52

相關問題