2011-01-14 43 views
0

我想修復一個遊戲的2D板。我已經修復了桂的其他面板,一切都很順利。但董事會的面板不能印在窗戶上。我有點困惑,因爲我認爲我遵循了與我需要的其他面板相同的想法。如何用Java代表遊戲板面板?

這裏是我做了什麼:

編輯: 什麼,我試圖做的是根據它的尺寸固定板面板的比賽中,保持每平方在一個數組爲了在任何需要的地方使用它。我用繪製方法繪製每個小方塊並將其放回面板。所以,棋盤上的每個方塊都是一個面板。這是主意。但是,你可以看到。它有麻煩/錯誤。

編輯:代碼已更新。 剛剛發現問題的一部分。我首先想到我已經設置了背景來平方,但我沒有。與這一個它出現在面板上一個寬黑色的「列」。不幸的是,仍然沒有廣場。 :(

編輯: 另外,我意識到draw方法從來沒有被調用過,當我把draw方法放在下面的方法中時我可以看到方塊但是它們仍然很小,我用setSize重新定義它們,但仍然沒有變化。

如何使用paint方法來正確地編輯面板????因爲它是現在不能,即使它不能返回一個對象(例如面板),因爲它是多態無效!

/** 
    *Method used to construct the square in the area of the 
    *gui's grid. In this stage a GUISquare array is being constructed, 
    * used in the whole game as 
    *a mean of changing a square graphical state. 
    *@param squares is the squares array from whom the gui grid will be 
    *constructed. 
    *@see getSquare about the correspondance beetween a squareModel and 
    * a GUISquare. 
    */ 
    private void initBoardPanel(SquareModel[][] squares){ 

     BoardPanel.setLayout(new GridLayout(height ,width)); //set layout 

     SquareRenderer[][] Squares; 
     JPanel[][] grid; 
     Squares=new GUISquare[height][width()]; 
     grid=new JPanel[height()][width()]; 

     for (int i=0; i<height(); i++){ 
       for (int j=0; j<width() ; j++){ 
        SquareRenderer kou=new SquareRenderer(i,j); 
       kou.setSquare(myGame.getSquares()[i][j]); 
         //NOTE: THE FOLLOWING DRAW METHOD CANT BE CALLED!!!? 
       if (myGame.getSquares()[i][j] instanceof SimpleSq ){ 
         kou .paintPanel(i,j,"");} 
       else if (myGame.getSquares()[i][j] instanceof ActionSq ) 
       {  kou .paintPanel(i,j); 
       } 
        //JUST BECAUSE DRAW CANT BE CALLED I PUT ITS CODE HERE: 
        //JUST TO CHECK: 
       JPanel panel = new JPanel(); 
       panel.setLayout(new BorderLayout()); 
       JLabel label1 = new JLabel("Move To "+myGame.getSquares()[i][j].getGoTo()); 
       JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare()); 

       panel.setBackground(Color.ORANGE); 
       panel.add(label2, BorderLayout.NORTH); 
       panel.add(label1, BorderLayout.CENTER); 
       panel.setSize(250,250); 

        ///////// <--until here ---paint method<--- 

       kou.add(panel); 
       kou.setVisible(true); 
       kou.setBackground(Color.BLACK); 

       Squares[i][j]= kou; 

       BoardPanel.add(kou); 
       BoardPanel.setVisible(true); 
       BoardPanel.setBackground(Color.WHITE); 
       } 
     } 
     this.add(BoardPanel,BorderLayout.WEST); 
    // this.pack(); //sets appropriate size for frame 
     this.setVisible(true); //makes frame visible 
} 

SQUARERENDERER實施:

/** 
* Transformer for Snake/Ladder 
* <br>This method is used to display a square on the screen. 
*/ 
public void paintPanel(int i,int j) { 
    JPanel panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    JLabel label1 = new JLabel("Move To"+myGame.getSquares()[i][j].getGoTo()); 
    JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare()); 
    JSeparator CellSeparator = new JSeparator(orientation); 
    panel.add(CellSeparator); 
    panel.setForeground(Color.ORANGE); 
    panel.add(label2, BorderLayout.NORTH); 
    panel.add(label1, BorderLayout.CENTER); 
} 
+6

建議:不是檢查廣場的類型,而是讓每個類實現一個`drawSquare`方法,它接受一個`GUISquare`。它使您的電路板初始化方法更清晰,並更好地封裝您的代碼。 – unholysampler 2011-01-14 19:09:14

+0

這會更好,你是對的。我剛剛離開了「清理」後的代碼。現在我只想結束圖形部分:/ thnx提示anw – FILIaS 2011-01-14 19:17:00

回答

2

我看到一對夫婦的事情是有問題的:

  1. 當使用佈局管理器,你應該避免調用的setSize。大多數佈局管理者將簡單地覆蓋它。您應該使用setPreferredSize,setMinimumSize和setMaximumSize,它們向佈局管理器提供提示。
  2. 它看起來不像SquareRenderer的繪製方法曾將任何創建的面板添加到任何東西。您應該將面板添加到繪製的最後一行中的SquareRenderer,或者(更好地)將子部件直接添加到SquareRenderer中。