2015-10-21 81 views
0

編輯:這個錯誤只發生在我在我的Ubuntu 14.04筆記本電腦上運行我的比賽。當我在Windows學校計算機上運行它時,它工作正常。正常運行時,當過我運行它,它落後於像瘋了似的的Java窗口落後於Ubuntu的但不是窗口時代碼不落後

窗口滯後,但滯後停止,而我調整窗口的大小與我的鼠標

我試圖做一個簡單的遊戲在Java中,但是,我查看了代碼,並使用System.out.println和System.currentTimeMillis進行了一些檢查,代碼運行時間遠遠少於1毫秒,所以這不是問題。

程序停止的時候我拖到調整窗口的大小,不斷改變窗口(大概強制屏幕重繪)的大小,它有很好的流暢的動畫效果當我這樣做。

當我的程序運行時,它以平滑的動畫開始,然後大約1秒鐘後它變爲大約2 FPS,然後另一秒鐘後變爲1 FPS,然後大約0.5FPS並停留在那裏直到我強制通過調整窗口

我的代碼重繪是:

public class WindowSetup extends JPanel{ 
private static final long serialVersionUID = 6486212988269469205L; 
static JFrame frame; 
static JPanel panel; 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    GameClass.draw(g); 
} 
public static void main(String[] args) { 
    frame = new JFrame("Evolver"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(800, 600); 
    panel = new WindowSetup(); 
    frame.add(panel); 
    panel.setVisible(true); 
    frame.setVisible(true); 

    GameClass.init(); 
    ActionListener al = new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
      GameClass.tick(); 
      panel.repaint(); 
     } 
    }; 
    Timer timer = new Timer(50,al); // lower numbers here make it run faster, but it still ends up slowing down to 1FPS 
    timer.start(); 
} 
} 

我GameClass:

public class GameClass { 
static Random random; 
static ArrayList<Enemy> enemies = new ArrayList<Enemy>(); 
static Wall wall; 
static void init(){ 
    random = new Random(); 
    wall=new Wall(); 
    for(int i=0;i<5;i++){ 
     enemies.add(new Enemy(random.nextInt(200)+100,random.nextInt(200)+100,10,10,(float) (random.nextFloat()*(Math.PI*2)),1)); 
    } 
} 
static void tick(){ 
    for(Enemy d:enemies){ 
     d.tick(); 
    } 
} 
static void draw(Graphics g){ 
    for(Enemy d:enemies){ 
     d.draw(g); 
    } 
    wall.draw(g); 
} 
} 

敵對階級:

public class Enemy { 
float x; 
float y; 
float width; 
float height; 
float direction; 
float speed; 

public Enemy(float x, float y, float width, float height, float direction, float speed) { 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
    this.direction = direction; 
    this.speed = speed; 
} 

public void tick() { 
    direction += 0.01; 
    if (!(GameClass.wall.collides((int) (x + (Math.cos(direction) * speed)), (int) (y + (Math.sin(direction) * speed)),(int)width,(int)height))) { 
     x += Math.cos(direction) * speed; 
     y += Math.sin(direction) * speed; 
    } 
} 

public void draw(Graphics g) { 
    g.drawRect((int) x, (int) y, (int) width, (int) height); 
} 
} 

牆代碼:

public class Wall { 
ArrayList<Rectangle> rectlist = new ArrayList<Rectangle>(); 

public Wall() { 
    // create list of walls here 
    rectlist.add(new Rectangle(10, 10, 50, 400)); 
    rectlist.add(new Rectangle(410,10,50,400)); 
    rectlist.add(new Rectangle(60,10,350,50)); 
    rectlist.add(new Rectangle(60,360,350,50)); 
} 

public void draw(Graphics g) { 
    for (Rectangle d : rectlist) { 
     g.drawRect(d.x, d.y, d.width, d.height); 
    } 
} 
public boolean collides(int x,int y,int width,int height){ 
    for(Rectangle d:rectlist){ 
     if(d.intersects(x, y, width, height)){ 
      return true; 
     } 
    } 
    return false; 
} 
} 
+0

我們可以看看你的'GameClass'的內容嗎?如果它隨着時間的推移而下降,那麼你很可能沒有清理你應該在的地方。 –

+0

這真的是基本的移動代碼,我添加了我的GameClass和Enemy。 GameClass.wall.collides()只是一個基本的矩形碰撞檢查器,它絕對不會產生滯後。 –

+1

調整框架的大小會在您拖動時調用'repaint()',並添加到由'Timer'排隊的那些框架中。 – trashgod

回答

1

有一個問題與Linux的圖形調度意味着它由於某種原因變得越來越慢。解決此問題的方法是在執行panel.repaint()後使用Toolkit.getDefaultToolkit()。sync(),這將停止放慢速度。

這個函數似乎沒有幫助其他操作系統上的任何東西,但它確實佔用了寶貴的CPU週期(實際上其中很多),所以使用System.getProperty(「os.name」)來確定是否你需要使用它。

+0

既然你解決了你的問題,請接受你自己的答案來結束這個問題。 – MalaKa

+0

它不會讓我再做2天 –

0

作爲一種變通方法,我想你可以 '模擬' 通過添加setVisible(false)setVisible(true)在你的動作偵聽調整大小的行爲:

public void actionPerformed(ActionEvent e) { 
     panel.setVisible(false); 
     GameClass.tick(); 
     panel.repaint(); 
     panel.setVisible(true); 

    } 
+0

DO NOT DO運行它!重複:不要做!這凍結了我的整個電腦,我不得不使用學校電腦,而我的解凍 –

+0

哇,你能發佈你的電腦的規格?它真的很奇怪,它會因此而凍結。 – javaguest

+0

這段代碼每秒被調用100次,請記住。這是一個英特爾核心i5-3230m CPU @ 2.60GHz×4。我有8 GB的RAM和1TB的磁盤空間。我的筆記本電腦是1歲,並運行ubuntu 14.04 –