0

I'm trying to make a chess game, but I can't figure out how to smoothly redraw my cells. I want them to be redrawn every time a Piece moves. I tried to just keep adding Jframes, but it makes it glitch back to old frames. Is there a simple way to just update some part of the GUI?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;






public class GUI {

    public static JFrame frame;                     //global variable for drawn frame
    public static ChessBoardPane tiles;


    public GUI(final Piece[][] board) {             //GUI for Board and Pieces



        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                frame = new JFrame("Chess");                                //initialize frame
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       //closes on exit
                drawFrame(board);                                           //draw frame
            }
        });


    }

    public void drawFrame(final Piece[][] board){                                               //updates gui with a new frame  

        try{
        tiles=new ChessBoardPane(board);
        frame.add(tiles);                       //add chess board to frame
        frame.pack();                                               //resizes frame to fit chess grid
        frame.setLocationRelativeTo(null);                          //window in middle of screen
        frame.setVisible(true);                                     //visible
        } catch(Exception ex){

        }


    }



    public class ChessBoardPane extends JPanel{                         //creates a grid of jButtons

        public ChessBoardPane(Piece board[][]) {
            int index = 0;
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            for (int row = 0; row < Board.width; row++) {
                for (int col = 0; col < Board.height; col++) {
                    Color color = index % 2 == 0 ? Color.LIGHT_GRAY : Color.DARK_GRAY;      //cells are dark and light gray
                    gbc.gridx = col;                    
                    gbc.gridy = row;
                    String symbol;
                    if(board[col][Board.height-row-1]!=null){
                        symbol=board[col][Board.height-row-1].symbol;                       //chooses the correct picture for a piece
                    }
                    else
                        symbol=null;

                    Cell tile = new Cell(color,symbol);
                    tile.addActionListener(new ButtonActionListener(col, Board.height-row-1));
                    add(tile, gbc);                                     //add cell to gui
                    index++;
                }
                index++;
            }
        }


    private class ButtonActionListener implements ActionListener{                   //used to get x and y coordinates for button presses and call methods 
        private int x;
        private int y;

        public ButtonActionListener(int x, int y){
            this.x=x;
            this.y=y;
        }

        public void actionPerformed(ActionEvent e){                                 //runs commands when a tile is pressed
                Game.attemptMove(x,y);
        }
    }


    }

    public class Cell extends JButton { //sets up cell behavior

        int x;
        int y;

        public Cell(Color background, String symbol) {

            setContentAreaFilled(false);      
            if(symbol!=null){
                ImageIcon img = new ImageIcon(symbol);                                  //puts picture in cell
                setIcon(img);
            }
            setBorderPainted(false);
            setBackground(background);                                                  //sets cell color
            setOpaque(true);


        }

        @Override
        public Dimension getPreferredSize() {                                           //sets size of cell
            return new Dimension(75, 75);                                           
        }

    }

}
Chris
  • 155
  • 1
  • 9
  • 1
    See also this [chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Sep 25 '14 at 04:09
  • 1
    *"can't figure out how to smoothly redraw my cells"* Given the GUI uses buttons, simply set a new icon for the button (like the above example). General tips: 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Sep 25 '14 at 04:12
  • 1
    Create a mapping between the board and the buttons, so that each button has a one-to-one relationship with the board. When the board is updated, each button (or at least the two buttons involved in the exchange) should be updated accordingly. Don't add and remove content all the time, don't rebuild the frame, don't show multiple frames... – MadProgrammer Sep 25 '14 at 04:20

0 Answers0