2016-12-06 44 views
5

我在Ubuntu 14.4.1中編寫了一個簡單的Java動畫程序。在JPanel內移動的球。但在執行過程中,JPanel中的球移動得相當不平穩。直到我將鼠標移動到JPanel中,這個問題纔會繼續。在JPanel內部移動鼠標時,球的運動非常平滑。應該說,我已經在Windows 10中運行該程序,並沒有發生任何問題。我的程序代碼如下:在Linux中運行JERKY的Java動畫程序

import java.awt.*; 
import javax.swing.*; 

public class BouncingBall extends JPanel { 
    Ball ball = new Ball(); 

    void startAnimation() { 
     while(true) { 
      try { 
       Thread.sleep(25); 
       ball.go(); 
       repaint(); 
      } catch(InterruptedException e) {} 
     } // end while(true) 
    } // end method startAnimation() 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     ball.draw(g); 
    } // end method paintComponent 


    // inner class Ball 
    class Ball { 
     int x; 
     int y; 
     int diameter = 10; 
     int xSpeed = 100; 
     int ySpeed = 70; 

     void go() { 
      x = x + (xSpeed*25)/1000; 
      y = y + (ySpeed*25)/1000; 

      int maxX = getWidth() - diameter; 
      int maxY = getHeight() - diameter; 
      if(x < 0) { 
       // bounce at the left side 
       x = 0; 
       xSpeed = -xSpeed; 
      } else if(x > maxX) { 
       // bounce at the right side 
       x = maxX; 
       xSpeed = -xSpeed; 
      } else if(y < 0) { 
       // bounce at the top side 
       y = 0; 
       ySpeed = -ySpeed; 
      } else if(y > maxY) { 
       // bounce at the bottom size 
       y = maxY; 
       ySpeed = -ySpeed; 
      } // end if-else block 
     } // end method go() 

     void draw(Graphics g) { 
      g.fillOval(x , y , diameter , diameter); 
     } // end method draw 
    } // end inner class Ball 


    public static void main(String[] args) { 
     JFrame window = new JFrame(); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     BouncingBall animation = new BouncingBall(); 
     animation.setPreferredSize(new Dimension(500 , 500)); 
     animation.setBackground(Color.white); 
     window.add(animation); 
     window.pack(); 
     window.setVisible(true); 

     animation.startAnimation(); 
    } // end method main 
} // end class BouncingBall 

什麼問題?我必須在我的Ubuntu中更改一些設置嗎? 我也把一些測試代碼的paintComponent方法中,如下所示:

protected void paintComponent(Graphics g) { 
    System.out.println("paintComponent call number: " + counter); 
    ++counter; 
    super.printComponent(g); 
    ball.draw(g); 
} 

在類MovingBall聲明的變量0計數器的初始值。我觀察到每秒調用paintComponent的次數遠遠超過JPanel實際刷新率。

+0

您的paintComponent方法中的println會減慢速度,並且可能會有明顯的效果。你會希望它離開那裏。 –

+0

添加println之前出現的主要問題。 println聲明僅僅是一個測試語句,可以詳細瞭解這個問題。 – Hedayat

+0

我也會在你的動畫方法中獲得實時增量,並根據實時增量計算精靈的最佳位置。這樣,如果動畫快或慢,球就會移動相同的距離。 –

回答

6

默認情況下在Windows中啓用視頻加速,但在Linux中默認情況下未啓用。 (這對於多年來一直如此,我可以發誓,這個默認改變爲最新的Java版本,但顯然我錯了。)

可以enable OpenGL得到加速性能:

public static void main(String[] args) { 
    System.setProperty("sun.java2d.opengl", "true"); 

    JFrame window = new JFrame(); 

或者,您可以在命令行上設置該屬性:

java -Dsun.java2d.opengl=true BouncingBall 
+0

謝謝你的回答。我嘗試了你告訴的兩種方式。但他們都沒有解決這個問題。和以前一樣,動畫仍然很生澀。 – Hedayat

+0

這個屬性對我有很大的影響。你的Linux系統的顯卡是什麼?你知道它是否提供OpenGL加速? – VGR

+0

我不知道這兩個圖形卡和支持OpenGL。我如何理解他們? – Hedayat