2

ive created a basic GUI for Reversi using JPanels to represent the board in a GridLayout. At the moment when a piece is played the square that is clicked changes colour. Ive been trying to get a circular piece instead to change and the background to stay the same.

Ive searched around quite a bit and I can't seem to find a way to do this?

--Edit--

The code for the constructor. When a piece is played a Mouse listener just updates the board

Public boardGUI(int num){

    game = new reversiGame(num, false);
    Dimension boardSize = new Dimension(600, 600);

    numSquares = num; 

    layeredPane = new JLayeredPane();
    getContentPane().add(layeredPane);
    layeredPane.setPreferredSize(boardSize);
    layeredPane.addMouseListener(this);

    board = new JPanel();
    layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);

    board.setLayout( new GridLayout(numSquares, numSquares) );
    board.setPreferredSize( boardSize );
    board.setBounds(0, 0, boardSize.width, boardSize.height);

    for (int i = 0; i < (numSquares * numSquares); i++) {
        JPanel square = new JPanel( new BorderLayout() );
        square.setBorder(BorderFactory.createLineBorder(Color.black));
        square.setBackground(Color.green);
        board.add( square );

        int row = (i/numSquares);
        int col = (i % numSquares);

        if ((row + 1 == numSquares / 2 & col + 1 == numSquares/2) || row == numSquares/2 & col == numSquares/2){
            square.setBackground(Color.white);
        }

        if ((row + 1 == numSquares / 2 & col == numSquares/2) || row == numSquares/2 & col + 1 == numSquares/2){
            square.setBackground(Color.black);
        }
     }  
}

The updateBaord function

public void updateBoard(){
    int x = 0;
    int y = 0;

    ImageIcon black = new ImageIcon("Images/large-black-sphere.ico");
    ImageIcon white = new ImageIcon("Images/large-white-sphere.ico");
    for(int i = 0; i < numSquares; i++){
        for(int j = 0; j < numSquares; j++){

            x = i * (600/numSquares);
            y = j * (600/numSquares);
            Component c =  board.findComponentAt(x, y);
            GridType g = game.getGridType(i, j);

            if (g.equals(GridType.WHITE)){
                JPanel temp = (JPanel) board.getComponent( i + j );
                piece = new JLabel(white);
                temp.add(piece);
                //c.setBackground(Color.white);
            }
            else if(g.equals(GridType.BLACK)){
                JPanel temp = (JPanel)board.getComponent( i + j );
                piece = new JLabel(black);
                temp.add(piece);
                //c.setBackground(Color.black);
            }
            else{
                //c.setBackground(Color.GREEN);
            }



        }
    }

}
CNevin561
  • 143
  • 1
  • 3
  • 12
  • 6
    Show us the code that you're using, or something that may be able to help us out. There's not enough information here. – Deco Nov 06 '11 at 14:11
  • 1
    One way is to simply draw or not draw an oval in the paintComponent method of a JPanel or other JComponent. Key points are to separate your game logic from your game GUI, and use the logic or model as the basis for deciding what circles get drawn where. – Hovercraft Full Of Eels Nov 06 '11 at 14:29

2 Answers2

2

Add JLabel to each grid on the game board. Then you can use Icons to represent the reversi pieces. Then when you want to change the reversi pieces you change the Icon of the label.

The ChessBoard example here: How do I make my custom Swing component visible? shows how this might be done.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Ive changed the updateBoard function in my GUI to add the JLabel which is constructed using an ImageIcon though when i run it it doesnt show up. Any thoughts? – CNevin561 Nov 06 '11 at 17:51
  • @CNevin561, so how is your code different than my code? I posted a complete working example. I suggested you change the Icon not create a new label. – camickr Nov 07 '11 at 02:18
2

… using an ImageIcon, though when I run it, it doesn't show up.

You may need to invoke repaint(); ColorIcon in MVCGame is an example.

Looking closer, your updateBoard() method appears to be adding a new label to an existing panel without either removing the old label or validating the panel's layout. Instead, update the Icon in place.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045