2012-03-04 58 views
2

我正在做一個大型的壟斷遊戲作爲一個項目在學校,並遇到了一個相當大的性能問題。現在我有一種繪製方法,每次它被調用時就繪製整個板子......這是一個大問題,因爲板子只需要在開始時繪製一次,並且只有在某人買房子或什麼時才需要繪製。我想要繪製的唯一組件是玩家,因爲它們是最左右移動並需要繪製的組件。Java鞦韆遊戲板

這裏是我的板的paint方法:

public void paint(Graphics g){ 
     super.paint(g); 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     //Draw all the spots 
     for(int i = 0; i < spots.length; i++){ 
      g2d.setColor(Color.white); 
      g2d.fill(spots[i].getRect()); 
      if(spots[i] instanceof Property){ 
       if(i < 10){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width,spots[i].getRect().height/3); 
        g2d.fill(temp); 
        g2d.setColor(Color.black); 
        g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().height); 
       }else if(i >= 10 && i < 20){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x+spots[i].getRect().width-spots[i].getRect().width/3,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height); 
        g2d.fill(temp); 
       }else if(i >= 20 && i < 30){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height-spots[i].getRect().height/3,spots[i].getRect().width,spots[i].getRect().height/3); 
        g2d.fill(temp); 
        g2d.setColor(Color.black); 
        g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().y); 
       }else if(i >= 30 && i < 40){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height); 
        g2d.fill(temp); 
       } 
      }else if(spots[i] instanceof Railroad){ 
       if(i == 5) 
        g2d.drawImage(imgTrain3, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null); 
       else if(i == 15) 
        g2d.drawImage(imgTrain4, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null); 
       else if(i == 25) 
        g2d.drawImage(imgTrain1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null); 
       else if(i == 35) 
        g2d.drawImage(imgTrain2, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null); 
      }else if(spots[i] instanceof Chance){ 
       if(i == 7) 
        g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y, null); 
       else if(i == 22) 
        g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null); 
       else if(i == 36) 
        g2d.drawImage(imgChance3, spots[i].getRect().x, spots[i].getRect().y, null); 
      }else if(spots[i] instanceof Community){ 
       if(i == 2) 
        g2d.drawImage(imgComm1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null); 
       else if(i == 17) 
        g2d.drawImage(imgComm2, spots[i].getRect().x, spots[i].getRect().y, null); 
       else if(i == 33) 
        g2d.drawImage(imgComm3, spots[i].getRect().x+spots[i].getRect().width/3, spots[i].getRect().y, null); 
      }else{ 
       g2d.setColor(spots[i].getColor()); 
       g2d.fill(spots[i].getRect()); 
      } 
     } 
     //Draw the outline of every spot 
     g2d.setColor(Color.black); 
     for(Spot index : spots) 
      g2d.draw(index.getRect()); 
     //Draw the outline of the whole board 
     g2d.drawRect(newX, newY, boardSize, boardSize); 
     //Draw the Players location 
     for(Player index : players) 
      g2d.drawImage(index.getImage(), index.getLoc().x, index.getLoc().y, null); 
    } 

基本上一噸的文本,用於代表董事會的,而這個正在做,每次董事會重繪。有什麼建議?

獎勵問題:我也剛剛開始爲玩家在移動之後開始製作移動動畫(目前只是跳到目的地)。我創造了一個計時器,每次擲出1秒(例如:如果擲出5秒,需要5秒鐘移動)。唯一的問題是,我不確定如何顯示玩家片從開始位置到結束位置的緩慢移動。只需要有人給我一個基本的想法,這樣我就可以朝正確的方向前進。

+0

+1,太棒了。我只是在尋找一個讓我開始的想法..壟斷遊戲對我的新項目非常出色。 – ApprenticeHacker 2012-03-04 14:03:46

+0

謝謝!這絕對是一個有趣的遊戲開發和觀看,因爲它們都彙集在一起​​。 – mbreen 2012-03-04 14:15:10

回答

4

將電路板漆成BufferedImage。在繪畫方法中,畫出棋盤的圖像,然後將棋子畫在上面。

順便說一句 - 使用Swing時,不要在頂層容器中繪製,而應使用JComponentJPanel。對於後兩者,請覆蓋paintComponent(Graphics)而不是paint(Graphics)

+0

因此,它仍然會在每次重新繪製調用時繪製板,但是因爲它是一張圖像,所以效率更高?目前,當玩家移動時,我可以看到可見的延遲,這對於BufferedImage來說會不太明顯? – mbreen 2012-03-04 14:29:01

+1

@mbreen:試試看:-)。是的,它會更快,並且您的Java窗口被其他窗口遮擋時,您將需要偶爾繪製整個板子。 – 2012-03-04 14:40:56

+0

如果抽真空需要很長時間,是的。但是,每次調用paint方法時都應該可以繪製數百個對象,而不會顯着延遲。也許最好看一下發布[SSCCE](http://sscce.org/),這樣我們就可以更仔細地看待使用的整個過程。此外,強烈建議追蹤toto2找到的教程中的任何渲染示例。嘗試增加他們從繪畫1 ..球(線,..任何)繪畫他們的分數 - 並看看他們如何執行。 – 2012-03-04 14:41:25

1

您可以避免重新繪製整個表面:jComponent.repaint(someRectangle)。另請參閱此example

但是,您必須自己畫出需要在每次移動之間更新的矩形(或矩形)。你不僅需要新東西出現的矩形,還需要消失的地方。

對於Swing中的動畫,看看這個tutorial。你會注意到我在這篇文章中鏈接的例子都來自官方Swing tutorial