How to represent a Board Panel in Java for a game ?

Posted by FILIaS on Stack Overflow See other posts from Stack Overflow or by FILIaS
Published on 2011-01-14T18:52:43Z Indexed on 2011/01/14 23:53 UTC
Read the original article Hit count: 311

Filed under:
|
|
|

I wanna fix a 2D board for a game. I've already fixed other panels for the Gui and everything goes well. But the panel for the board cant be printed on the window. I'm a bit confused about it as i think i've followed the same ideas as for the others panels i need.

Here's what i've done:

EDIT:*EDIT* what i'm trying to do is fix a board panel for the game according to the dimensions of the it,hold every square in an array in order to use it after wherever it;s needed. I draw each little square of it with the method draw and put it back to the panel. So, each square on the board is a panel. This is the idea. But as u can see. There are troubles/errors on it.

EDIT: code updated. just found a part of the problem. i thought first that i had set background to squared, but i didnt. with this one it appears on the panel a wide black "column". Unfortunately,still none squares. :(

One More EDIT: Also,i realized that draw method is never called. when i put the draw method in the following method i can see the squares but they remain small. I redefine them with setSize but still no change.

How can I use paint method to edit the panels properly???? As it is now it can't. Even it can't return an object(eg panel) as it's polymorphic void!

/**
    *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
}







IMPLEMENTED BY 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);
}

© Stack Overflow or respective owner

Related posts about java

Related posts about gui