Java Graphics on java, 2d array board 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
19:53 UTC
Read the original article
Hit count: 285
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:
/**
*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(myGame.getHeight(),myGame.getWidth())); //set layout
Squares=new GUISquare[myGame.getHeight()][myGame.getWidth()];
grid=new JPanel[myGame.getHeight()][myGame.getWidth()];
for (int i=0; i< myGame.getHeight(); i++){
for (int j=0; j<myGame.getWidth() ; j++){
grid[i][j] = new JPanel( );
GUISquare kout=new GUISquare(i,j);
kout.setSquare(myGame.getSquares()[i][j]);
kout.draw(myGame.getSquares()[i][j].getGoTo(),myGame.getSquares()[i][j].getNumber()); /*draw method is been called. the first parameter is the number of
the square that the player will be moved to if lands in this one
square,the second parameter is just the number of the square */
kout.setVisible(true);
grid[i][j].add(kout);
grid[i][j].setVisible(true);
BoardPanel.add(grid[i][j]);
BoardPanel.setVisible(true);
BoardPanel.setBackground(Color.WHITE);
GUISquare temp=this.getSquare(squares[i][i]);
Squares[i][j]= temp;
}
}
this.add(BoardPanel,BorderLayout.WEST);
// this.pack(); //sets appropriate size for frame
this.setVisible(true); //makes frame visible
}
/**
* Transformer for Rand/Move
* <br>This method is used to display a square on the screen.
*/
public void draw(int goTo ,int number) {
JPanel panel = new JPanel();
JLabel label1 = new JLabel(""+"Move To"+goTo);
JLabel label2 = new JLabel(""+number);
JSeparator CellSeparator = new JSeparator(orientation);
panel.add(CellSeparator);
panel.setLayout(new BorderLayout());
panel.add(label1, BorderLayout.CENTER);
panel.add(label2, BorderLayout.LINE_START);
}
I've posted only one draw method...but all versions are alike.
© Stack Overflow or respective owner